Search code examples
node.jsgoogle-apigmail

Google API returns 400 BadRequest for batch request


I'm trying to send bath request to GMail API using request for nodejs. The problem is even trough I generate the same query as in documentation, gmail always respond with 400 BadRequest

Those are my headers in form of json

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

Example of body content

--c2db2692-8877-4159-bf02-03e6c5d3ffbf
Content-Type: application/http
Content-ID: <item1:[email protected]>


GET /gmail/v1/users/[email protected]/messages/14c6c0c43e9bb16b


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

Any suggestions why this happens? Request generates extra few spaces and make headers in lowercase, however I'm not sure if this is the problem.

Example of nodejs code

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:[email protected]>",
        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: This is clearly an issue with how I make my node call, send same request via HTTPTester and everything worked fine

UPD2: Decided to use bachelor, however I haven't figured what was causing this issue with plain request


Solution

  • Quite hard to troubleshoot such a problem. I would more than likely say it's down to the spaces. I had a similar issue that was because the request body was seen as invalid due to it's structure.

    Uppercase or lowercase headers shouldn't cause an issue.

    If it helps I've created a node module to creating batch requests: https://www.npmjs.com/package/batchelor. Feel free to use it.