Grails - withRest 响应不是 JSON

Grails - withRest response is not JSON

我正在使用 rest 插件 0.8 来执行 withRest post 请求:

withRest(id: "spark", uri: "https://my/Url/") {
   def bodyContent = ... 
   def response = post(path: 'myPath', body: bodyContent,
                       headers: [Authorization: "Basic dmhTZHZLY01hYjFFczFEsdfsdfoQzFMOE1IUmd6NUJ6aU86U0p3UcxXVXFLNElhRVhwOQ"],
                       requestContentType: "application/x-www-form-urlencoded")
}

响应应该是 JSON,因为我收到的响应已解析且键中没有所需的括号。 我试图添加一个 Accept header 但代码没有编译。我如何强制将响应返回为 JSON?

您是否验证了您的 api 正在返回具有正确内容类型 header 的响应(例如 application/json ?)然后其余客户端应该能够自动找出响应的内容类型并相应地解析它。

也试试这个

withRest(id: "spark", uri: "https://my/Url/", contentType:ContentType.JSON) {

}