将JSON传递到HTTP POST请求 [英] pass JSON to HTTP POST Request

查看:261
本文介绍了将JSON传递到HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 nodejs 请求向google QPX Express API [1]发出HTTP POST请求。 [2]。

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

我的代码如下:

    // 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)
        }
    })

我想要做的是使用multipart参数传递JSON [3]。
而不是正确的JSON响应,我得到一个错误(400未定义)。

What I'm trying to do is passing the JSON using the multipart argument [3]. But instead of the proper JSON response I got an error (400 undefined).

当我使用相同的JSON和API密钥使用CURL ,它工作正常。所以我的API密钥或JSON没有问题。

When I make a request using the same JSON and API Key using CURL instead, it works fine. So there's nothing wrong with my API key or JSON.

我的代码有什么问题?

EDIT ::

工作CURL示例:

到我的请求到一个名为request.json的文件:

i) I saved the JSON which I would pass to my request into 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)然后,在终端中我切换到新创建的目录request.json文件被找到并运行(myApiKey代表我的实际API密钥显然):

ii) then, in the terminal I switched to the directory in which the newly created request.json file was located and run (myApiKey stands for my actual API Key obviously):

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]为nodejs设计的http请求客户端: https://www.npmjs.org/package / request
[3]这里是一个我发现的示例 https://www.npmjs .org / package / request#multipart-related
[4] QPX Express API返回400解析错误

推荐答案

我认为以下应该工作: / p>

I think the following should work:

// fire request
request({
    url: url,
    method: "POST",
    json: true,
    headers: {
        "content-type": "application/json",
    },
    body: JSON.stringify(requestData)
}, ...

文档将使用

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

在这种情况下, Content-type:application / json 标题会自动添加。

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

这篇关于将JSON传递到HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆