Groovy 内置 REST/HTTP 客户端? [英] Groovy built-in REST/HTTP client?
本文介绍了Groovy 内置 REST/HTTP 客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说 Groovy 有一个内置的 REST/HTTP 客户端.我能找到的唯一库是HttpBuilder,是这个吗?
I heard that Groovy has a built-in REST/HTTP client. The only library I can find is HttpBuilder, is this it?
基本上,我正在寻找一种从 Groovy 代码内部执行 HTTP GET 的方法,而无需导入任何库(如果可能的话).但是由于这个模块似乎不是核心 Groovy 的一部分,我不确定我是否有正确的库.
Basically I'm looking for a way to do HTTP GETs from inside Groovy code without having to import any libraries (if at all possible). But since this module doesn't appear to be a part of core Groovy I'm not sure if I have the right lib here.
推荐答案
Native Groovy GET 和 POST
Native Groovy GET and POST
// GET
def get = new URL("https://httpbin.org/get").openConnection();
def getRC = get.getResponseCode();
println(getRC);
if (getRC.equals(200)) {
println(get.getInputStream().getText());
}
// POST
def post = new URL("https://httpbin.org/post").openConnection();
def message = '{"message":"this is a message"}'
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if (postRC.equals(200)) {
println(post.getInputStream().getText());
}
这篇关于Groovy 内置 REST/HTTP 客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文