groovy RESTClient的POST、GET、DELETE 用法

groovy 的常用操作 groovy全局变量、json操作、sql操作、http操作、多线程、集合 阅读详情


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:

  1. RESTClient has convenience methods for get, put post delete, head
  2. The response data is always parsed and buffered in-memory
  3. The returned HttpResponseDecorator instance gives convenient access to headers and the parsed response data
  4. No user-defined closure is needed

Test a URL using the HEAD method

import groovyx.net.http.RESTClient
import groovy.util.slurpersupport.GPathResult
import 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 == 200
assert 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 == 200
assert ( resp.data instanceof GPathResult ) // parsed using XmlSlurper
assert resp.data.text == msg
assert resp.data.user.screen_name == userName
def 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 == 200
assert resp.data.id == postID
println "Test tweet ID ${resp.data.id} was deleted."

原文地址: http://groovy.codehaus.org/modules/http-builder/doc/rest.html
Java 开发 2_0 通过 CouchDB 和 Groovy 的 RESTClient 实现 REST 立即下载

相关推荐

通过CouchDB和Groovy的RESTClient实现REST

在过去数年中,开源世界的创新发展将Java?开发人员的生产力提升了一个层次。免费的工具、框架和解决方案弥补了曾经一度匮乏的空缺。其中,被一些人认为是Web2.0数据库的ApacheCouchDB是非常有潜力的。全面掌握CouchDB并不困难,使用它就像使用Web浏览器一样简单。本期Java开发2.0专栏将介绍CouchDB并向您展示如何发挥Groovy的RESTClient的强大功能目前为止,本专栏系列已经探究了Google和Amazon平台中的云计算。虽然它们在实现和结构上存在不同,但两种平台都支持快速和可扩展的部署方式。它们可以快速经济地组装、测试、运行和维护Java应用程序,这无疑是前所

16-Groovy-GET/POST请求

Groovy-GET/POST请求

小白的博客 2638

解密CANoe数据回放:从BLF到ASC的格式转换与优化策略

本文深入解析CANoe数据回放中BLF与ASC格式的转换与优化策略,帮助工程师高效处理汽车电子测试数据。详细介绍了两种格式的特性、转换方法及性能优化技巧,包括CANoe内置功能、命令行自动化和第三方工具应用,提升故障诊断和系统验证效率。

weixin_28331555的博客 535

Groovy 发送 HTTP 请求

build.gradle里面想进行网络请求应该如何操作 get非常的简单 def res1 = new URL('https://httpbin.org/ip').text // or def res2 = 'https://httpbin.org/ip'.toURL().text post可以这么用 import groovy.json.JsonOutput import groovy.json.JsonSlurper /** * 发送 HTTP POST 请求 * @param ur

丿灬安之若死 6620

PLC控制气缸与电磁阀:从原理到实战编程详解

在工业自动化领域,很多工程师都能编写PLC程序控制电机启停,但当涉及到气缸和电磁阀的精确控制时,却常常遇到动作不协调、响应延迟甚至设备损坏的问题。这背后的关键不在于PLC编程本身,而在于对气动元件特性和控制逻辑的深入理解。 本文将从实际工程问题出发,深入解析气缸与电磁阀的工作原理、PLC控制逻辑设计、常见故障排查方法,并通过完整的梯形图实例,帮助读者掌握气动系统自动化编程的核心要点。无论你是刚接触PLC的初学者,还是希望提升气动控制技能的工程师,都能从中获得实用的技术指导。 1. 气缸与电磁阀在自动化控制中

weixin_42522374的博客 336

基于Meteoinfo与TrajStat的后向轨迹聚类分析实战:从数据准备到可视化制图

本文详细介绍了基于Meteoinfo与TrajStat进行后向轨迹聚类分析的完整实战流程。从Java环境配置、Meteoinfo软件与TrajStat插件安装,到精准获取NOAA气象数据、设置参数进行批量轨迹计算,再到利用聚类分析提炼主要气流路径,并最终完成专业地图可视化与制图。文章旨在帮助环境与气象研究者快速掌握从数据准备到成果输出的全链路操作。

p4q5r6s7t的博客 804

Java httpClient发送post请求工具类

Java httpClient发送post请求工具类 public static String send(String url, JSONObject jsonObject, String encoding) throws IOException { String body = ""; //创建httpclient对象 CloseableHttpClient client = HttpClients.createDefault(); //创

weixin_44021888的博客 1463

GenomeSurvyIndex设计--随笔017

摘要: GenomeSurvyIndex是一款自动化多倍体基因组调查工具,整合FastK、Smudgeplot和GenomeScope2.0三大核心组件。该工具通过k-mer频谱特征自动识别基因组倍性类型,包括二倍体(1:2:4)和同源四倍体(1:2:3:4),并采用3a1b>2a2b算法判定同源/异源多倍体。工具提供完整的conda环境配置方案,封装为Python脚本实现全流程自动化分析,包含k-mer计数、倍性判定、基因组特征计算等功能,最终输出标准化报告和可视化图表。该方案可直接部署使用,适用于

weixin_44874487的博客 473

Groovy RestClient 流自动关闭的问题

Groovy RestClient 自动关闭流的问题问题描述问题代码源码分析问题解决扩展 问题描述 groovy 中使用http-builder-0.72.jar读取图片字节流,遇到 Attempted read from closed stream异常。 问题代码 import groovyx.net.http.HTTPBuilder import groovyx.net.http.Method...

lg409966136的博客 613

从Xilinx FPGA到MIPI DSI LCD屏:手撸代码的实战经验分享

本文分享了使用Xilinx FPGA驱动MIPI DSI LCD屏的实战经验。作者从依赖官方IP核失败,到与底层D-PHY IP缠斗,最终通过手撸代码,直接配置GTY收发器原语并实现完整的D-PHY与DSI协议栈,成功点亮屏幕。文章详细剖析了协议细节、Vivado工具链的调试技巧与避坑指南,为FPGA工程师提供了从理论到实践的宝贵参考。

weixin_29188997的博客 399

2.4 提示词优化

我们在使用大模型的时候,同一任务使用不同表述的提示词,可能得到差异巨大的结果,因为模型的性能高度依赖输入提示词的质量。我们可以通过提示词优化来更高效地获取理想输出。

从0到1,突破自己 1008

RESTClient使用

import groovy.json.JsonOutput import groovyx.net.http.ContentType import groovyx.net.http.RESTClientRESTClient http = new RESTClient("http://localhost:8080/") def response = http.get(contentType : Cont

大山 4238

Ubuntu系统等保3级配置核查与加固

虽然这种方法在某些老旧的系统中仍然有效,但现代Linux发行版通常不推荐使用hosts.deny和hosts.allow文件来管理防火墙规则,因为它们的功能已经被更现代的防火墙管理工具(如iptables或firewalld)所取代。如果你想禁止特定的环境变量,可以从这个列表中移除它们,或者如果你想完全阻止所有环境变量的传递,可以将其设置为空。你可以通过ssh-keygen生成密钥对,并将公钥添加到用户的~/.ssh/authorized_keys文件中。修改配置后,需要重启SSH服务以使更改生效。

悦分享 183

http接口测试工具——RESTClient

https://www.cnblogs.com/jing1617/p/6430137.html   摘要: RESTClient是用java Swing编写的基于http协议的接口测试工具,工具比较灵巧,便于做接口的调试,源码在官网上可以下到,感兴趣的可以研究一下 WizTools.org RESTClient is a Java Swing application for testing ...

EminentBoy的博客 1410

Qt for WebAssembly环境搭建

一、前言 二、Ubuntu 1、在线安装Qt15 在线安装下载器:https://download.qt.io/official_releases/online_installers/ 安装时记得勾选WebAssembly,如下: 2、安装依赖 # Install Python sudo apt-get install python2.7 # Install CMake (optional, only needed for tests and building Binaryen) sudo apt

贝勒里恩的博客 3667

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

测试就是这么简单,rest-assured

关于rest-assuredrest-assured 是一个能够简化测试rest服务的Java DSL,像ruby或者python一样的动态语言去测试和验证http服务。基于java并且兼容了groovy动态语言的特性,使我们像写脚本语言一样去测试http服务。例如:你的http服务( http://localhost:8080/lotto/{id})返回一个如下json:{ "lotto":

u012516914的专栏 1万+

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

18、Camunda Script Task Groovy 发送POST请求

Groovy 发送POST Rest接口请求引入依赖流程Groovy脚本 发送POST请求开启流程案例Rest接口2、groovy 案例22.1、流程和groovy脚本2.2、流程开启设置变量 引入依赖 <!--Spring boot 集成camunda--> <dependency> <groupId>org.camunda.bpm.springboot</groupId> <artifa

红鹰 2057

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

使用GROOVY脚本语言发送get和post请求

使用GROOVY脚本语言发送get和post请求 2018年9月10日 用到的包 import org.apache.http.NameValuePair import org.apache.http.client.entity.UrlEncodedFormEntity import org.apache.http.client.methods.CloseableHttpRespons...

qq_38924181的博客 1万+

groovy的HttpUtils工具类post和get请求

import groovy.json.JsonOutput import org.apache.commons.lang.StringUtils import org.apache.http.HttpResponse import org.apache.http.NameValuePair import org.apache.http.client.HttpClient import org.a...

Xiazhiyu_whu的博客 3838

hbase和couchdb_使用CouchDB和Groovy的RESTClient进行REST

到目前为止,本专栏文章系列已经探索了使用Google和Amazon平台的云计算。 尽管它们的实现和结构不同,但这两个平台都可以实现快速且可扩展的部署。 它们使前所未有的快速,廉价地组装,测试,运行和维护Java应用程序成为可能。 但是,云并不是当今影响Java开发速度的唯一因素。 开源解决方案还使您能够快速组装软件应用程序,因为您不再需要编写太多代码。 编写自己的对象关系映射(ORM),日志记...

cusi77914的博客 344

Groovy和HTTP

本文最初发表在GroovyMag的 2012年12月号上。 Groovy使与Web交互更容易的一些不同方式 Groovy的主要好处之一是它简化了我们在Java中处理的一些常见方案。 带有条件,错误处理和许多其他问题的复杂代码可以非常简洁易懂的方式表示。 本文将介绍一些与通过HTTP与内容进行交互有关的便捷Groovy主义。 首先,我们将研究一些添加到标准Java类中的语法糖,这些语法可以...

1154

基于Groovy+HttpRestful的超轻量级的接口测试用例配置的设计方案及DEMO实现

目标 设计一个轻量级测试用例框架,接口测试编写者只需要编写测试用例相关的内容(入参及结果校验),不需要理会系统的实现,不需要写跟测试校验无关的内容。 思路 测试用例分析 一个用例由以下部分组成: (1) 测试用例名称 ; (2) 接口名及URL/Path; (3) 接口入参; (4) 接口返回结果校验。 测试框架需要读取用例配置信息,根据指定接口及入参调用服务,并根据指定校验函数来对接...

weixin_30588827的博客 359

groovy 操作http请求

话不多说,先上一段代码,如果你使用过jquery,看到这样的代码是不是很亲切呢? [code="java"] def http = new HTTPBuilder('http://www.baidu.com') http.request(GET,TEXT) { //设置url相关信息 uri.path='/' uri.query=[a:'1',b:2] //设置请求头信息...

给生活加点盐 3471

nGrinder中快速编写groovy脚本04-发送POST请求

POST请求分类:1、根据是否修改代码,分为两种方式:一种是在UI界面添加后自动生成脚本,一种是直接在脚本中添加2、根据请求参数的不同,主要可以分为两种:param为key value格式body为json格式一、通过UI方式发送POST请求–key/value参数通过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置当选择了请求方法为POST后,在高级配置中...

weixin_33937913的博客 501

groovy使用httpRequest获取token

groovy使用httpRequest获取token

运维@小兵的博客 1530
上一篇: 2011 程序员薪资调查报告全文发布
下一篇: Apache Solr 概念、安装与导入数据库中表格的数据到索引(适合新手)
欧德曼
博客等级 码龄19年 63粉丝 82原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值