Reputation: 103
java.
I use Unirest.post
to post my multipart data. but server shows error to me:
multipart: NextPart: EOF.
I find that, if I set Content-Length
I can solve this.
Here the code:
String buff = "my data";
HttpResponse<String> res = Unirest.post(url)
.header("Content-Type", multipart.getContentType().getValue())
.header("Content-Length", String.valueOf(buff.length()))
.body(buff).asString();
But after I add .header("Content-Length", String.valueOf(buff.length()))
, run java I get error:
org.apache.http.client.ClientProtocolException
How can I solve this?
Upvotes: 1
Views: 668
Reputation: 369
You need to remove the earlier set content length so that you can set a new one.
@John Rix's answer This code helped solved the problem
private static class ContentLengthHeaderRemover implements HttpRequestInterceptor{
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.removeHeaders(HTTP.CONTENT_LEN);// fighting org.apache.http.protocol.RequestContent's ProtocolException("Content-Length header already present");
}
}
HttpClient client = HttpClients.custom()
.addInterceptorFirst(new ContentLengthHeaderRemover())
.build();
Upvotes: 0