Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
“设置内容类型”;申请表/pdf";在Groovy RESTClient/HTTPBuilder中_Rest_Groovy_Content Type_Httpbuilder - Fatal编程技术网

“设置内容类型”;申请表/pdf";在Groovy RESTClient/HTTPBuilder中

“设置内容类型”;申请表/pdf";在Groovy RESTClient/HTTPBuilder中,rest,groovy,content-type,httpbuilder,Rest,Groovy,Content Type,Httpbuilder,我正在使用Groovy的RESTClient/HTTPBuilder库向Web服务发送GET和POST请求。一个资源需要内容类型为application/PDF的PDF。响应将是一个XML 请求-->申请后处理/pdf 响应您需要做的是定义一个自定义编码器。遵循以下(不完整)示例: import org.codehaus.groovy.runtime.MethodClosure import org.apache.http.entity.FileEntity //this part adds

我正在使用Groovy的RESTClient/HTTPBuilder库向Web服务发送GET和POST请求。一个资源需要内容类型为
application/PDF
的PDF。响应将是一个XML

请求-->申请后处理/pdf

响应您需要做的是定义一个自定义编码器。遵循以下(不完整)示例:

import org.codehaus.groovy.runtime.MethodClosure
import org.apache.http.entity.FileEntity

//this part adds a special encoder    
def client = new RESTClient('some host')
client.encoder.putAt('application/pdf', new MethodClosure(this, 'encodePDF'))

//here is the method for the encoder added above
HttpEntity encodePDF(File pdf) {
    new FileEntity(pdf)
}

请尝试上面的例子,让我知道它是否有效。

不幸的是,它不起作用。也许我做错了什么?这是我的代码:异常:
没有方法:encodePDF()的签名适用于参数类型:(org.apache.http.entity.FileEntity)
传递文件本身,而不是FileEntity。抱歉,可能已经太晚了:)你介意将其添加到上面的代码中吗?似乎我看不到树木的树木。我找不到和你有什么主要区别。但是我得到了一个例外。它不是
body:encodePDF(新文件('C:/temp/test.pdf'),
它应该是:
body:new文件('C:/temp/test.pdf'),
client.encoder.putAt('application/pdf', new MethodClosure(this, 'encodePDF'))
response = client.post(
  uri: url,
  body: new File('C:/temp/test.pdf'),
  requestContentType: 'application/pdf'
)

HttpEntity encodePDF(File pdf) {
  new FileEntity(pdf)
}
import org.codehaus.groovy.runtime.MethodClosure
import org.apache.http.entity.FileEntity

//this part adds a special encoder    
def client = new RESTClient('some host')
client.encoder.putAt('application/pdf', new MethodClosure(this, 'encodePDF'))

//here is the method for the encoder added above
HttpEntity encodePDF(File pdf) {
    new FileEntity(pdf)
}