QPX Express API is returning 400 parse error

2019-08-14 15:17发布

问题:

I'd like to consume data from the Google QPX Express API and I'm trying to setting up a very basic request. On the demopage [1] I copied the generated JSON which looks as follows:

{
  "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
  }
}

According to the Online Manual [2] a basic request has the following structure:

https://www.googleapis.com/qpxExpress/v1/trips/search?key=your_API_key_here

So I inserted this code using my API key which I generated in the google developers console, but the re

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "parseError",
                "message": "Parse Error"
            }
        ],
        "code": 400,
        "message": "Parse Error"
    }
}

What's wrong with my request?

[1] https://qpx-express-demo.itasoftware.com/ [2] https://developers.google.com/qpx-express/v1/requests

回答1:

Turns out, something was wrong with POSTMAN (REST API AddOn for Chrome). I tried the same using curl:

i) I saved the JSON which I would pass to my request into a file called "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) then, in the terminal I switched to the directory in which the newly created request.json file was located and run (myApiKey stands for my actual API Key obviously):

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey

This worked for me.



回答2:

You need to specify a value for "Content-Type" HTTP header in your request. In you case "application/json" :

Content-Type: application/json