I am trying to execute an HTTP POST request in the QIP X Express Express [1] API using nodejs and request [2].
My code is as follows:
// create http request client to consume the QPX API var request = require("request") // JSON to be passed to the QPX Express API var requestData = { "request": { "slice": [ { "origin": "ZRH", "destination": "DUS", "date": "2014-12-02" } ], "passengers": { "adultCount": 1, "infantInLapCount": 0, "infantInSeatCount": 0, "childCount": 0, "seniorCount": 0 }, "solutions": 2, "refundable": false } } // QPX REST API URL (I censored my api key) url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey" // fire request request({ url: url, json: true, multipart: { chunked: false, data: [ { 'content-type': 'application/json', body: requestData } ] } }, function (error, response, body) { if (!error && response.statusCode === 200) { console.log(body) } else { console.log("error: " + error) console.log("response.statusCode: " + response.statusCode) console.log("response.statusText: " + response.statusText) } })
What I'm trying to do is pass JSON with the multipart [3] argument. But instead of the correct JSON answer, I got an error (400 undefined).
When I make a request using the same JSON and API key using CURL, it works fine. So nothing happened with my API key or JSON.
What is wrong with my code?
EDIT
CURL working example:
i) I saved the JSON that I would pass to my request in a file called "request.json":
{ "request": { "slice": [ { "origin": "ZRH", "destination": "DUS", "date": "2014-12-02" } ], "passengers": { "adultCount": 1, "infantInLapCount": 0, "infantInSeatCount": 0, "childCount": 0, "seniorCount": 0 }, "solutions": 20, "refundable": false } }
ii), then in the terminal I switched to the directory in which the newly created request.json file was created and launched (myApiKey clearly indicates my actual API key):
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey
[1] https://developers.google.com/qpx-express/ [2] http request client designed for nodejs: https://www.npmjs.org/package/request [3] here is an example I found https://www.npmjs.org/package/request#multipart-related [4] QPX Express API returns 400 parsing errors