带有JSON的Google QPX API帖子在Groovy上返回“HTTP / 1.1 403 Forbidden”

时间:2015-04-19 18:47:53

标签: json groovy httpbuilder

我无法在groovy中使用Google rest API。

我是groovy的新手:S我正在使用HTTPBuilder来询问服务。我的代码是:

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

我尝试使用curl示例并且有效:

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

" reques.json"的内容是:

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

我使用的密钥是一个网络密钥。

我会继续搜索和尝试。

1 个答案:

答案 0 :(得分:1)

在groovy代码中,您似乎将地图添加到网址,而不是将其发送到请求正文中?

要在groovy中添加身体,请更改

uri.query = ... 

开始
body = ...