简体   繁体   English

apache cxf MultiPart请求没有Content-Length标头

[英]apache cxf MultiPart request has no Content-Length header

I'm having below code to send a multipart/form-data request. 我有下面的代码来发送多部分/表单数据请求。

List<Attachment> multipartData = new ArrayList<>();
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"file\"; 
filename="+fileObj.getName());

FileInputStream inputStream = new FileInputStream(fileObj);
multipartData.add(new Attachment("file",inputStream, cd1));

MultipartBody multipart = new MultipartBody(multipartData);

In my RestClient class, I'm using the below lines of code to send a POST request using JAX-RS Client object 在我的RestClient类中,我使用下面的代码行使用JAX-RS Client对象发送POST请求。

if ("POST".equals(method)) {
            response = this.client.getBuilder().post(Entity.entity(entity,MediaType.MULTIPART_FORM_DATA));

I checked the HTTP request body using Wiremock and is as below: 我使用Wiremock检查了HTTP请求正文,如下所示:

Transfer-Encoding: [chunked]
Accept: [*/*]
Cache-Control: [no-cache]
User-Agent: [Apache-CXF/3.2.5]
Connection: [keep-alive]
Host: [127.0.0.1:9990]
Pragma: [no-cache]
Content-Type: [multipart/form-data; boundary="uuid:04b491f5-50de-4f4f-b7c0-cd745136d3d1"]

--uuid:04b491f5-50de-4f4f-b7c0-cd745136d3d1
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <file>
Content-Disposition: form-data; name="file"; filename=sample.txt

<File content goes here>

I want to know how the content-length header is missing in the request payload. 我想知道请求有效载荷中缺少内容长度标头。 Is there any way to set the content-length header to the request? 有什么方法可以将content-length标头设置为请求吗?

Please help me. 请帮我。

I used the apache cxf WebClient to unset the transfer encoding as chunked. 我使用apache cxf WebClient取消将传输编码设置为分块。

if ("POST".equals(method)) {
    Invocation.Builder builder = this.client.getBuilder();
    WebClient.getConfig(builder).getHttpConduit().getClient().setAllowChunking(false);
    response = builder.post(Entity.entity(entity,MediaType.MULTIPART_FORM_DATA));
}

With this, the client is able to send the request with content-length header. 这样,客户端就可以发送带有content-length标头的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM