Groovy中的HTTPBuilder和MultipartEntitymultipart表单数据

Groovy中的HTTPBuilder和MultipartEntitymultipart表单数据,第1张

概述尝试模拟需要将一些INPUT / TEXT字段与文件中的数据组合在一起的HTTP POST.看起来我可以有一个或另一个,但不是两个? 在下面的代码段中,paramsToPost = [name:’John’,年龄:22] @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0 尝试模拟需要将一些input / TEXT字段与文件中的数据组合在一起的http POST.看起来我可以有一个或另一个,但不是两个?

在下面的代码段中,paramstopost = [name:’John’,年龄:22]

@Grab(group='org.codehaus.groovy.modules.http-builder',module='http-builder',version='0.5.0')Boolean dohttpPost(String url,Map paramstopost,String fileContent) {    httpBuilder http = new httpBuilder(url)    def resp = http.request(Method.POST ) { req ->        multipartentity mpe = new multipartentity()        mpe.addPart "foo",new StringBody(fileContent)        req.entity = mpe        // body = paramstopost // no such property    }    println "response: ${resp}"    return true}

有人有工作样品吗?

解决方法 刚刚让我的代码与旧的commons-httpclIEnt-3.1.jar一起工作

(new httpBuilder(url)).request(Method.POST) { request ->multipartentity mpe = new multipartentity(httpMultipartMode.broWSER_COMPATIBLE);mpe.addPart('fileinput',new StringBody(params.fileinput))if (params.fileinput=='file')    mpe.addPart('file1',new inputStreamBody(uploadedfile.inputStream,uploadedfile.ContentType,uploadedfile.originalfilename))else if (params.fileinput=='text')    mpe.addPart('fileText',new StringBody(params.fileText))mpe.addPart('Tags1',new StringBody(params.Tags1)) request.entity = mperequest.getParams().setParameter("http.connection.timeout",http_TIMEOUT)request.getParams().setParameter("http.socket.timeout",http_TIMEOUT)response.success = { resp,reader ->    render(text : "Successfully uploaded file\n\n${reader.text}")}response.failure = { resp ->  render (status: 500,text: "http Failure Accessing Upload Service ${resp.statusline}" )}

希望这可以帮助

总结

以上是内存溢出为你收集整理的Groovy中的HTTPBuilder和MultipartEntity / multipart表单数据全部内容,希望文章能够帮你解决Groovy中的HTTPBuilder和MultipartEntity / multipart表单数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231540.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存