调试错误请求400错误(Debugging Bad Request 400 error)
我试图从谷歌的QPX Express API中返回一系列航班,无论我遇到一个糟糕的请求响应:
{ StatusCodeError: 400 - {"error":{"errors":[{"domain":"global","reason":"badRequest","message":"Invalid inputs: received empty request."}],"code":400,"message":"Invalid inputs: received empty request."}}我如何接近请求的结构有什么问题吗? 我正在使用request-promise库
const options = { method: 'POST', uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXX', qs: { "request": { "passengers": { "adultCount": 1 }, "slice": [{"origin": "BOS", "destination": "LAX", "date": "2017-03-01" }] } }, json: true } request(options) .then(function (response) { console.log(response) }) .catch(function (err) { console.log(err) })I'm trying to return a list of flights from Google's QPX Express API, howver I'm stumped on a bad request response:
{ StatusCodeError: 400 - {"error":{"errors":[{"domain":"global","reason":"badRequest","message":"Invalid inputs: received empty request."}],"code":400,"message":"Invalid inputs: received empty request."}}Is there something wrong with how I'm approaching the structure of the request? I'm using the request-promise library
const options = { method: 'POST', uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXX', qs: { "request": { "passengers": { "adultCount": 1 }, "slice": [{"origin": "BOS", "destination": "LAX", "date": "2017-03-01" }] } }, json: true } request(options) .then(function (response) { console.log(response) }) .catch(function (err) { console.log(err) })最满意答案
核实:
const options = { method: 'POST', uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXX', qs: { "request": { "passengers": { "adultCount": 1 }, "slice": [{"origin": "BOS", "destination": "LAX", "date": "2017-03-01" }] } }, json: true }; request(options) .then(function (response) { console.log(response); }) .catch(function (err) { console.log(err); });你忘了结束uri字符串。 另外请不要忘记分号。
编辑:尝试:
request({ url: (your url here), method: "POST", json: requestData },其中requestData将是你的qs。
I have solved the issue. The request needed to include the data in the body key with the content-type set to JSON.
This now returns data from the API as expected.
const options = { method: 'POST', uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?&key=XXXXXXXXXXXXXXXXXXXX', body: { "request": { "passengers": { "adultCount": "1" }, "slice": [ { "origin": "SFO", "destination": "LAX", "date": "2017-06-19" } ], "solutions": "1" } }, json: true } request(options) .then(function (response) { console.log(response.trips.tripOption[0].saleTotal) }) .catch(function (err) { console.log(err) })更多推荐
发布评论