Pass JSON to HTTP POST Request

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

+59
json curl express node-request
Nov 28 '14 at 2:08
source share
6 answers

I think the following should work:

 // fire request request({ url: url, method: "POST", json: requestData }, ... 

In this case, the header Content-type: application/json automatically added.

+129
Nov 28 '14 at 14:25
source

I have been working on this for too long. The answer that helped me was: send Content-Type: application / json post with node.js

The following format is used:

 request({ url: url, method: "POST", headers: { "content-type": "application/json", }, json: requestData // body: JSON.stringify(requestData) }, function (error, resp, body) { ... 
+13
Apr 27 '15 at 19:55
source

You do not want to use a multiple but simple POST request (with Content-Type: application/json ). 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 }); 
+7
Nov 28 '14 at 14:26
source

Now with the new JavaScript version (ECMAScript 6 http://es6-features.org/#ClassDefinition ) there is a better way to send requests using nodejs and Promise request ( http://www.wintellect.com/devcenter/nstieglitz/5-great -features-in-es6-harmony )

Using the library: https://github.com/request/request-promise

 npm install --save request npm install --save request-promise 

client:

 //Sequential execution for node.js using ES6 ECMAScript var rp = require('request-promise'); rp({ method: 'POST', uri: 'http://localhost:3000/', body: { val1 : 1, val2 : 2 }, json: true // Automatically stringifies the body to JSON }).then(function (parsedBody) { console.log(parsedBody); // POST succeeded... }) .catch(function (err) { console.log(parsedBody); // POST failed... }); 

Server:

 var express = require('express') , bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.json()); app.post('/', function(request, response){ console.log(request.body); // your JSON var jsonRequest = request.body; var jsonResponse = {}; jsonResponse.result = jsonRequest.val1 + jsonRequest.val2; response.send(jsonResponse); }); app.listen(3000); 
+3
May 17 '17 at 1:33 pm
source
  var request = require('request'); request({ url: "http://localhost:8001/xyz", json: true, headers: { "content-type": "application/json", }, body: JSON.stringify(requestData) }, function(error, response, body) { console.log(response); }); 
+2
Mar 24 '17 at 10:14
source

According to the doc: https://github.com/request/request

Example:

  multipart: { chunked: false, data: [ { 'content-type': 'application/json', body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}}) }, ] } 

I think you are sending an object where a string is expected, replace

 body: requestData 

 body: JSON.stringify(requestData) 
+1
Nov 28 '14 at 14:20
source



All Articles