如何使用标准库在Groovy中发布JSON? [英] How to post JSON in Groovy with standard libraries?
本文介绍了如何使用标准库在Groovy中发布JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只使用标准库,在常规模式下将JSON发布到URL的最简单方法是什么? (即,不使用HttpClient或HttpClientBuilder)
这是一个脚本,我不希望链接到其他许多库中。
$ b解决方案
def con =http:// endpointUrl.toURL()。openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty(Content-Type,application / json);
con.outputStream.withWriter {writer - >
作家<< jsonString
}
字符串响应= con.inputStream.withReader {Reader reader - > reader.text}
What's the easiest way to post JSON to a URL in groovy using only the standard libraries? (ie, without using HttpClient or HttpClientBuilder)
This is a script I'd rather not have to link in lots of other libraries to.
解决方案
def con = "http://endpointUrl".toURL().openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.outputStream.withWriter { writer ->
writer << jsonString
}
String response = con.inputStream.withReader { Reader reader -> reader.text }
这篇关于如何使用标准库在Groovy中发布JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文