昨天研发反馈使用EDI的HTTP调用结点时,http响应报错如下: Failed to parse multipart servlet requestnested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found 。
EDI在构建http请求头时设置了 Content-Type 为“multipart/form-datacharset=UTF-8”,缺失了 boundary ,导致了如上错误。
正确的Content-Type格式应该为:“multipart/form-databoundary=<calculated when request is sent>”
设置Camel Body的时候需要一起设置header,最终发送请求的header格式类似:
上面的Content-Type是postman内置的,下面的是手动加上的。
实验:
1)两者都勾选,上传成功
2)只勾选手动添加的Content-Type,上传失败,错误同上
3)只勾选内置的Content-Type,上传成功
首先,标准HTTP协议对上传文件等表单的定义在这里:wwwietforg/rfc/rfc1867txt 大概数据包格式如下:单文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-dataname="field1"
Joe Blow
--AaB03x
content-disposition: form-dataname="pics"filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
多文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-dataname="field1"
Joe Blow
--AaB03x
content-disposition: form-dataname="pics"
Content-type: multipart/mixed, boundary=BbC04y
--BbC04y
Content-disposition: attachmentfilename="file1.txt"
其次,python上传文件的几种方法:
1 自己封装HTTP的POST数据包:http//stackoverflowcom/questions/680305/using-multipartposthandler-to-post-form-data-with-python
import httplibimport mimetypesdef post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = httplib.HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() def encode_multipart_formdata(fields, files): LIMIT = '----------lImIt_of_THE_fIle_eW_$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + LIMIT) L.append('Content-Disposition: form-dataname="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)