调试错误请求400错误(Debugging Bad Request 400 error)

编程入门 行业动态 更新时间:2024-10-28 16:20:13
调试错误请求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) })

更多推荐

本文发布于:2023-08-02 07:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Debugging   error   Request   Bad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!