groovy RESTClient的POST、GET、DELETE 用法

groovy RESTClient的POST、GET、DELETE 用法,第1张

概述RESTClient is an extension of HTTPBuilder, which makes a few concessions in HTTPBuilder's flexibility in order to make REST operations as simple as possible. RESTClient makes great use of the automati


RESTClIEnt is an extension of httpBuilder,which makes a few concessions in httpBuilder's flexibility in order to make REST operations as simple as possible.

RESTClIEnt makes great use of the automatic content-type parsing and enCoding which makes working with XML and JsON extremely easy,both in the request and response sIDe. It also adds some additional convenIEnce methods for response header parsing.

The main advantages of RESTClIEnt are:

RESTClIEnt has convenIEnce methods for getput post deletehead The response data is always parsed and buffered in-memory The returned HttpResponseDecorator instance gives convenIEnt access to headers and the parsed response data No user-defined closure is needed
Test a URL using the head method
import groovyx.net.http.RESTClIEntimport groovy.util.slurpersupport.GPathResultimport static groovyx.net.http.ContentType.URLENC twitter = new RESTClIEnt( 'https://twitter.com/statuses/' )// twitter auth omitted try { // expect an exception from a 404 response:    twitter.head path : 'public_timeline'    assert false,'Expected exception'}// The exception is used for flow control but has access to the response as well:catch( ex ) { assert ex.response.status == 404 } assert twitter.head( path : 'public_timeline.Json' ).status == 200


The above example takes advantage of httpBuilder's default failure handler,which will cause an exception to be thrown for any 'Failed' response. That exception will still allow access to details of the response (e.g. the response status or message).

GET our frIEnds' timeline
def resp = twitter.get( path : 'frIEnds_timeline.Json' )assert resp.status == 200assert resp.ContentType == JsON.toString()assert ( resp.data instanceof net.sf.Json.JsON )assert resp.data.status.size() > 0

All request parameters are defined here.

The resp fIEld in the above example is an instance of httpResponseDecorator. Calling resp.getData() returns the parsed response content. This is the same parsed response that you would get passed to httpBuilder's response handler closure,but it is always buffered in-memory and the response stream is automatically closed.


POST a status update to Twitter!
def msg = "I'm using httpBuilder's RESTClIEnt on ${new Date()}" resp = twitter.post( path : 'update.xml',body : [ status:msg,source:'httpbuilder' ],requestContentType : URLENC ) assert resp.status == 200assert ( resp.data instanceof GPathResult ) // parsed using XmlSlurperassert resp.data.text == msgassert resp.data.user.screen_name == usernamedef postID = resp.data.ID.toInteger()

Note that the above example is posting the request data as application/x-www-form-urlencoded. (The twitter API doesn't support XML or JsON POST requests.) For this reason,a requestContentType parameter must be specifIEd in order to IDentify how the request body should be serialized.

Also note that we're requesting update.xml,not update.Json. Since we never set a default content-type on the RESTClIEnt instance or pass a ContentType argument in this request,RESTClIEnt will put Accept: */* in the request header,and parse the response based on whatever is given in the response content-type header. So because Twitter correctly IDentifIEs its response as application/xml,it will automatically be parsed by the default XML parser.


Now DELETE that post
resp = twitter.delete( path : "destroy/${postID}.Json" )assert resp.status == 200assert resp.data.ID == postIDprintln "Test tweet ID ${resp.data.ID} was deleted."

原文地址: http://groovy.codehaus.org/modules/http-builder/doc/rest.html 总结

以上是内存溢出为你收集整理的groovy RESTClient的POST、GET、DELETE 用法全部内容,希望文章能够帮你解决groovy RESTClient的POST、GET、DELETE 用法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1266900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存