来自 the docs 的帖子示例不适用于 http-builder 1.7.1。
def msg = "I'm using HTTPBuilder's RESTClient on ${new Date()}"
def resp = twitter.post(
path : 'update.json',
body : [ status:msg, source:'httpbuilder' ],
requestContentType : URLENC )
assert resp.status == 200
assert resp.headers.Status
assert resp.data.text == msg
def postID = resp.data.id
异常(exception)是
wslite.rest.RESTClientException: No such property: body for class: wslite.http.HTTPRequest
通过 API 进行测试,你应该如何正确构建帖子并不明显。有什么想法吗?
最佳答案
使用 httpbuilder 调用需要登录的页面时遇到困难。因此分享我的工作代码。
def http = new HTTPBuilder("http://localhost:8080/")
def query = [ username: "testUsername", password:"testPassword"]
http.request(Method.POST,ContentType.URLENC) {
uri.path = "user/signin"
uri.query = query
headers['Content-Type']= "application/x-www-form-urlencoded"
response.success = {resp-> println resp.statusLine }
}
希望这有帮助。
关于groovy - 如何使用 Groovy HTTPBuilder RESTClient 构建 HTTP post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25453929/