我使用groovy RESTClient 0.6来发出POST请求.我希望响应中有一个XML有效负载.我有以下代码:
def restclient = new RESTClient('<some URL>')
def headers= ["Content-Type": "application/xml"]
def body= getClass().getResource("/new_resource.xml").text
/*
If I omit the closure from the following line of code
RESTClient blows up with an NPE..BUG?
*/
def response = restclient.post(
path:'/myresource', headers:headers, body:body){it}
println response.status //prints correct response code
println response.headers['Content-Length']//prints 225
println response.data //Always null?!
Run Code Online (Sandbox Code Playgroud)
response.data始终为null,即使我使用Google Chrome的postman客户端尝试相同的请求时,我也会返回预期的响应主体.这是RESTClient的已知问题吗?
如何使用 HTTPBuilder 在 groovy 中执行 HTTP post,上传文件的原始字节而不使用 multipart/form-data?具体来说,我希望我的请求如下所示:
POST http://....
Host: myhost
Content-Length: numBytes
Proxy-Connection: Keep-Alive
Raw Data
Run Code Online (Sandbox Code Playgroud) 我的请求看起来像这样,但是当我尝试运行此代码时,出现此错误:
@Grab(group='org.codehaus.groovy.modules.httpbuilder',module='http-builder', version='0.7')
import groovy.json.JsonOutput
import groovy.json.JsonBuilder
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*
import groovyx.net.http.HttpResponseException
public PaymentSpring() throws Exception {
def username ='test_XXXXXXXXXXXXXXXXXXXX'
def resp
def https = new RESTClient('https://api.paymentspring.com/api/v1')
https.auth.basic(username,'')
def charge= [
card_owner_name : 'test tset',
card_number : '345829002709133',
card_exp_month:'12',
card_exp_year : '2015',
csc:'999',
amount:'100.00',
email_address:'ujdieu@yahoo.com',
send_receipt : true
]
try {
resp = https.post(
path:'/charge',
requestContentType: URLENC,//request content synchronously for the "headers"
headers: ['Content-Type': 'application/x-www-form-urlencoded'],
body:charge
)
} catch(HttpResponseException ex) {
println ex
}
println resp
Run Code Online (Sandbox Code Playgroud)
结果:
groovyx.net.http.HttpResponseException: …Run Code Online (Sandbox Code Playgroud) 我正在尝试这样安装HTTPBuilder:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2')
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
java.lang.RuntimeException:捕获Grapes时出错-[下载失败:commons-lang#commons-lang; 2.4!commons-lang.jar]
$ grape -V resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.2
:: loading settings :: url = jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
no default ivy user dir defined: set to /home/zoran/.ivy2
including url: jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings-public.xml
no default cache defined: set to /home/zoran/.ivy2/cache
including url: jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings-shared.xml
including url: jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings-local.xml
including url: jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings-main-chain.xml
including url: jar:file:/home/zoran/.gvm/groovy/2.4.4/lib/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings-default-chain.xml
settings loaded (22ms)
default cache: /home/zoran/.ivy2/cache
default resolver: default
-- 5 resolvers:
shared [file]
default [chain] [local, main]
local [file]
public [ibiblio]
main [chain] [shared, public] …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Groovy HTTPBuilder设置连接超时,并且在我的生活中无法找到方法.
使用普通ol'URL很容易:
def client = new URL("https://search.yahoo.com/search?q=foobar")
def result = client.getText( readTimeout: 1 )
Run Code Online (Sandbox Code Playgroud)
抛出一个SocketTimeoutException,但这不是我想要的.出于各种原因,我宁愿使用HTTPBuilder或更好的RESTClient.
这确实有效:
def client = new HTTPBuilder()
def result = client.request("https://search.yahoo.com/", Method.GET, "*/*") { HttpRequest request ->
uri.path = "search"
uri.query = [q: "foobar"]
request.getParams().setParameter("http.socket.timeout", 1);
}
Run Code Online (Sandbox Code Playgroud)
但是不推荐使用request.getParams().
对于我的生活,我找不到将正确的RequestConfig注入构建器的方法.
如何在Grails中安装和使用httpbuilder插件?
我正在尝试使用HTTPBuilder类将XML数据发布到URL.目前我有:
def http = new HTTPBuilder('http://m4m:aghae7eihuph@m4m.fetchapp.com/api/orders/create')
http.request(POST, XML) {
body = {
element1 {
subelement 'value'
subsubelement {
key 'value2'
}
}
}
response.success = { /* handle success*/ }
response.failure = { resp, xml -> /* handle failure */ }
}
Run Code Online (Sandbox Code Playgroud)
经过检查,我发现请求确实以XML作为正文.我有3个问题.首先,它省略了经典的xml行:
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
必须在主体的顶部,其次内容类型不设置为:
application/xml
Run Code Online (Sandbox Code Playgroud)
最后,对于XML中的一些元素,我需要设置属性,例如:
<element1 type="something">...</element1>
Run Code Online (Sandbox Code Playgroud)
但我不知道如何以上述格式执行此操作.有谁有想法怎么样?或者可能是另一种方式?
我有一个具有以下代码的常规文件:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
Run Code Online (Sandbox Code Playgroud)
我正在尝试下载要使用的依赖项HTTPBuilder。
但这向我显示了以下错误:
Error grabbing Grapes -- [download failed: org.apache.httpcomponents#httpclient;4.2.1!httpclient.jar,download failed: org.apache.httpcomponents#httpcore;4.2.1!httpcore.jar, download failed: commons-codec#commons-codec;1.6!commons-codec.jar, download failed: commons-beanutils#commons-beanutils;1.8.0!commons-beanutils.jar, download failed: commons-lang#commons-lang;2.4!commons-lang.jar]
Run Code Online (Sandbox Code Playgroud)
我尝试在命令提示符下使用以下命令解决此Grape依赖项:
grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.2
Run Code Online (Sandbox Code Playgroud)
以及尝试以下方法:
grape install org.codehaus.groovy.modules.http-builder http-builder 0.7.2
Run Code Online (Sandbox Code Playgroud)
然后它也向我显示了相同的错误。您能帮我解决这个问题吗?我使用Groovy版本2.2.2
我开发了一些我想在grails应用程序中使用的Web服务.可以使用Get或POST协议调用这些服务.
我已经看到我需要使用HTTP构建器对象来做到这一点.
这是我的代码:
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
def http = new HTTPBuilder( 'http://localhost:8086' )
http.request( GET, JSON ) {
uri.path = '/RegistrationService/webresources/users/isRegistered'
uri.query = [ login:'aa', password: 'bb' ]
response.success = { resp, xml ->
def xmlResult = xml
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是在Netbeans中我每次导入都有一个错误:无法解析类groovyx.net.http.HTTPBuilder无法解析类groovyx.net.http.ContentType ...
但是我尝试运行应用程序,这是我运行代码时的错误:
| Error 2013-02-25 23:33:32,596 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [POST] /WordGame/user/authenticate
No such property: uriPath for class: groovyx.net.http.HTTPBuilder$RequestConfigDelegate. Stacktrace follows:
Message: No such property: uriPath for …Run Code Online (Sandbox Code Playgroud) 我对RESTful api感到困惑. 码:
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
def isMailer = new HTTPBuilder( 'http://mailer-api.com' )
isMailer.request( GET, JSON ) {
uri.path = '/is/mail/rest/json/' + token
isMailer.auth.basic 'ddd', 'aaa'
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
response.success = { resp, json ->
// response handler for a success response code:
System.out << json
if(json.has("DISP_NAME")) {
println "************************"
res = "Yes"
} else if (json.has("ListError")) {
res = "No"
}
}
}
// handler for any failure status code: …Run Code Online (Sandbox Code Playgroud) httpbuilder ×10
groovy ×9
grails ×3
post ×2
dependencies ×1
file ×1
httpclient ×1
json ×1
request ×1
rest-client ×1
upload ×1
web-services ×1
xml ×1