简体   繁体   中英

Debugging Bad Request 400 error

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

Check it:

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

You forgot to end the uri string. Also please don't forget the semicolons.

Edit: Try:

request({
    url: (your url here),
    method: "POST",
    json: requestData
},

where requestData will be your 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)
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM