我的httpbuilder post和json的响应处理有一些问题
在我的服务中,我有:
def jsonDataToPost = '{"accountNumber" : ' + accNo + ',"accountName" : ' + accName + '}'
def http = new HTTPBuilder('https://myurl.com/dataInput')
def jsonResponse
http.auth.basic ('username','password')
http.request(POST, ContentType.JSON) {
headers.'Content-Type' = 'application/json'
body = jsonDataToPost
response.success = { json ->
println("Success")
jsonResponse = json
}
response.failure = { json ->
println("Fail")
jsonResponse = json
}
}首先,由于某些原因,代码实际上是跳过而不是完成,所以我没有得到我想要的jsonReponse,但我不知道为什么?如果我回复了我的响应.SUCCESS/fail,并且我发布了正确的数据,那么我的json post就可以工作了,但是我仍然得不到json的回复
发布于 2015-08-03 21:44:05
尝尝这个,
def requestData = [foo:bar]
http.request(POST, ContentType.JSON) {
headers.'Content-Type' = 'application/json'
body = (requestData as JSON).toString()
response.success = { resp, reader ->
println("Success")
jsonReponse = reader.text
}
response.failure = { resp, reader ->
println("Failed, status: " + resp.status)
jsonReponse = reader.text
}
}发布于 2021-02-13 23:38:38
如果您使用的是Grails 3.x,则可以使用http-builder-ng https://http-builder-ng.github.io/http-builder-ng
https://stackoverflow.com/questions/31788700
复制相似问题