pass JSON to HTTP POST Request
I think the following should work:
// fire requestrequest({ url: url, method: "POST", json: requestData}, ...
In this case, the Content-type: application/json
header is automatically added.
I worked on this for too long. The answer that helped me was at:send Content-Type: application/json post with node.js
Which uses the following format:
request({ url: url, method: "POST", headers: { "content-type": "application/json", }, json: requestData// body: JSON.stringify(requestData) }, function (error, resp, body) { ...
You don't want multipart, but a "plain" POST request (with Content-Type: application/json
) instead. Here is all you need:
var request = require('request');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 }};request('https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey', { json: true, body: requestData }, function(err, res, body) { // `body` is a js object if request was successful});