pass JSON to HTTP POST Request

后端 未结 9 2025
傲寒
傲寒 2020-11-29 18:22

I\'m trying to make a HTTP POST request to the google QPX Express API [1] using nodejs and request [2].

My code l

相关标签:
9条回答
  • 2020-11-29 19:15

    you can pass the json object as the body(third argument) of the fetch request.

    0 讨论(0)
  • 2020-11-29 19:17

    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
    });
    
    0 讨论(0)
  • 2020-11-29 19:18

    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) { ...
    
    0 讨论(0)
提交回复
热议问题