我正在尝试从api返回一些数据,但是当我点击服务器时,我收到一条错误的请求消息。我认为问题在于我的JSON Stringify,我是否正确地使用了这个函数来连接我的请求体?
输出:
{ request:
{ passengers: { kind: 'qpxexpress#passengerCounts', adultCount: 1 },
slice: [ [Object] ],
saleCountry: 'GB',
ticketingCountry: 'GB',
solutions: 10 } }
Upload successful! Server responded with: {
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Invalid inputs: received empty request."
}
],
"code": 400,
"message": "Invalid inputs: received empty request."
}
}
代码:
var express = require('express')
var router = express.Router()
var request = require('request')
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' })
})
let flightRequest = {
"request": {
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"slice": [{
"kind": "qpxexpress#sliceInput",
"origin": "LHR",
"destination": "OSL",
"date": "2016-12-03",
"permittedDepartureTime": {
"kind": "qpxexpress#timeOfDayRange",
"earliestTime": "06:00",
"latestTime": "11:00"
}}],
"saleCountry": "GB",
"ticketingCountry": "GB",
"solutions": 10
}
}
console.log(JSON.stringify("hello" + flightRequest))
JSON.stringify(flightRequest)
console.log(flightRequest)
request.post({url:'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXXXXXXXXXXX', flightRequest: flightRequest}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
module.exports = router
答案 0 :(得分:0)
我认为您需要按如下方式发布请求
request({
url: 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXXXXXXXXXXX',
method: "POST",
json: JSON.stringify(flightRequest)
}, function optionalCallback(err, httpResponse, body) { ...
或者可能是
request.post({
url: 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXXXXXXXXXXX',
json: JSON.stringify(flightRequest)
}, ...
答案 1 :(得分:0)
我是否正确使用此功能来连接我的请求体?
此代码
let flightRequest = { "request": { "passengers": { "kind": "qpxexpress#passengerCounts", "adultCount": 1 }, "slice": [ { "kind": "qpxexpress#sliceInput", "origin": "LHR", "destination": "OSL", "date": "2016-12-03", "permittedDepartureTime": { "kind": "qpxexpress#timeOfDayRange", "earliestTime": "06:00", "latestTime": "11:00" } } ], "saleCountry": "GB", "ticketingCountry": "GB", "solutions": 10 } } let flightAppend = {"appended text":"hello"} var obj = Object.assign(flightAppend, flightRequest) console.log(JSON.stringify(obj))
生成此输出
{ "appended text": "hello", "request": { "passengers": { "kind": "qpxexpress#passengerCounts", "adultCount": 1 }, "slice": [ { "kind": "qpxexpress#sliceInput", "origin": "LHR", "destination": "OSL", "date": "2016-12-03", "permittedDepartureTime": { "kind": "qpxexpress#timeOfDayRange", "earliestTime": "06:00", "latestTime": "11:00" } } ], "saleCountry": "GB", "ticketingCountry": "GB", "solutions": 10 } }
我不知道这是您正在寻找的内容,或者您正在使用的API的哪一方,但输出是有效的JSON
与生成" hello [object Object]"
的原始代码相对