【问题标题】:Google API returns 400 BadRequest for batch requestGoogle API 为批处理请求返回 400 BadRequest
【发布时间】:2015-03-31 08:40:23
【问题描述】:

我正在尝试使用 request 为 nodejs 发送洗澡请求到 GMail API。问题是即使我生成与文档中相同的查询,gmail 总是以 400 BadRequest

响应

这些是我的 json 格式的标题

{ 'Content-Type': 'multipart/mixed; boundary=c2db2692-8877-4159-bf02-03e6c5d3ffbf',
  authorization: 'Bearer MY_TOKEN',
  'content-length': 231 }

正文内容示例

--c2db2692-8877-4159-bf02-03e6c5d3ffbf
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>


GET /gmail/v1/users/sneg0k32@gmail.com/messages/14c6c0c43e9bb16b


--c2db2692-8877-4159-bf02-03e6c5d3ffbf--

任何建议为什么会发生这种情况? Request 会生成额外的几个空格并将标头设为小写,但我不确定这是否是问题所在。

nodejs代码示例

request({
    method: 'POST',
    uri: 'https://www.googleapis.com/batch',
    auth: {
      bearer: key
    },
    headers: {
      'Content-Type': 'multipart/mixed'
    },
    multipart: _.map(queries, function(query) {
      return {
        'Content-Type': 'application/http',
        'Content-ID': "<item1:12930812@barnyard.example.com>",
        body: 'GET ' + query
      }
    })
  }, function(error, response, body) {

    console.log(response.request.headers)
    _.map(response.request.body, function(chunk) {
      console.log(chunk.toString())
    })

    console.log(body)

  })

UPD:这显然是我如何进行节点调用、通过 HTTPTester 发送相同请求并且一切正常的问题

UPD2:决定使用单身汉,但是我还没有弄清楚是什么导致了这个问题的简单请求

【问题讨论】:

    标签: node.js google-api gmail


    【解决方案1】:

    很难解决这样的问题。我很可能会说这取决于空间。我遇到了类似的问题,因为请求正文由于其结构而被视为无效。

    大写或小写标题不应引起问题。

    如果有帮助,我创建了一个节点模块来创建批处理请求:https://www.npmjs.com/package/batchelor。随意使用。

    【讨论】:

    • 我遇到了 batchelor 的问题,它希望响应内容类型具有边界字段,但并不总是存在 (batchelor.js:170)。我现在将创建问题
    • 其实库本身没有问题,只是文档有误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2012-08-21
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多