ฉันกำลังพยายามส่งคำขอ HTTP POST ไปยัง google QPX Express API [1] โดยใช้nodejsและ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)
        }
    })
สิ่งที่ฉันกำลังพยายามทำคือส่ง JSON โดยใช้อาร์กิวเมนต์หลายส่วน [3] แต่แทนที่จะตอบสนอง JSON ที่เหมาะสมฉันได้รับข้อผิดพลาด (400 ไม่ได้กำหนด)
เมื่อฉันส่งคำขอโดยใช้ JSON และคีย์ API เดียวกันโดยใช้ CURL แทนก็ใช้ได้ดี ดังนั้นจึงไม่มีอะไรผิดปกติกับคีย์ API หรือ JSON ของฉัน
รหัสของฉันผิดอะไร
แก้ไข :
ตัวอย่าง CURL ที่ใช้งานได้:
i) ฉันบันทึก JSON ซึ่งฉันจะส่งต่อไปยังคำขอของฉันลงในไฟล์ชื่อ "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 จริงของฉันอย่างชัดเจน)
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 ที่ออกแบบมาสำหรับ nodejs: https://www.npmjs.org/package/request [3] นี่คือตัวอย่างที่ฉันพบhttps://www.npmjs.org/package/request#multipart-related [4] QPX Express API ส่งคืนข้อผิดพลาด 400 การแยกวิเคราะห์