Search code examples
jsongroovyhttpbuilder

Google QPX API post with JSON return "HTTP/1.1 403 Forbidden" on Groovy


i m not able to consume Google rest API in groovy.

I m newbie with groovy :S and i m using HTTPBuilder to ask the service. My code is:

 public static void testJSONPost() {
 def builder = new HTTPBuilder("https://www.googleapis.com/qpxExpress/v1/trips/search?key={$MY_WEB_KEY}")
     def result = builder.request(POST, JSON) { req ->
      uri.query = ["request": ["passengers": ["adultCount": 1],"slice": [["origin": "BOS","destination": "LAX","date": "2015-05-13"],["origin": "LAX","destination": "BOS","date": "2015-05-23"]]]]
             response.success = {resp, json ->
                     println "JSON POST Success: ${resp.statusLine}"

                     return json.name
             }

             response.failure = {resp ->
                     println "JSON POST Failed: ${resp.statusLine}"
             }
     }
}

testJSONPost()

I tried with curl example and it worked:

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

The content of "reques.json" is:

 {
  "request": {
    "passengers": {
      "adultCount": 1
    },
    "slice": [
      {
        "origin": "BOS",
        "destination": "LAX",
        "date": "2015-05-13"
      },
      {
        "origin": "LAX",
        "destination": "BOS",
        "date": "2015-06-06"
      }
    ]
  }
}

The key that i m using is a web key.

Well i ll keep on searching and trying.


Solution

  • In the groovy code, you seem to be adding the map to the url, not sending it in the body of the request?

    To add to the body in groovy, change your

    uri.query = ... 
    

    line to start with

    body = ...