Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何使用groovyx.net.http.RESTClient的post方法_Json_Grails - Fatal编程技术网

Json 如何使用groovyx.net.http.RESTClient的post方法

Json 如何使用groovyx.net.http.RESTClient的post方法,json,grails,Json,Grails,我有一个RESTClient类,我想发布到web服务。 我的POST请求将使用JSON。 因此,在Groovy类中,调用如下所示: def restClient = new RESTClient(url) def bodyContent = "{\"products\":[{\"ProductId\":1,\"ProductName\":\"Product\"}]}" response = restClient.post( headers:['Autho

我有一个RESTClient类,我想发布到web服务。 我的POST请求将使用JSON。 因此,在Groovy类中,调用如下所示:

 def restClient = new RESTClient(url)     
 def bodyContent = "{\"products\":[{\"ProductId\":1,\"ProductName\":\"Product\"}]}"

 response = restClient.post(
            headers:['Authorization': auth],
            contentType : jsonContentType,
            body: bodyContent
 )
标头和contenttype字符串是正确的。 我将JSON数据作为字符串类型放在bodyContent中,并放在POST请求的主体中

我运行此方法并得到错误:

at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setPropertiesFromMap(HTTPBuilder.java:1111)
at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.<init>(HTTPBuilder.java:946)
at groovyx.net.http.RESTClient.post(RESTClient.java:140)
at groovyx.net.http.RESTClient$post.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
位于groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setPropertiesFromMap(HTTPBuilder.java:1111)
位于groovyx.net.http.HTTPBuilder$RequestConfigDelegate。(HTTPBuilder.java:946)
位于groovyx.net.http.RESTClient.post(RESTClient.java:140)
位于groovyx.net.http.RESTClient$post.call(未知源)
位于org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
所以我想知道如何使用RESTClient发出POST请求?
我在这里查阅文件。POST示例不适用于我的情况。

您是否在测试用例中运行客户端?你能给这个问题增加更多的内容吗
jsonContentType
url
etcI将其作为普通类运行,而不是在测试用例中运行。jsonContentType只是一个string=“application/json”,url是一个提供REST服务的本地主机,它与curl命令配合得很好。您使用的是哪个版本的
HTTPBuilder