Should I set the Content-Type header for a MultipartEntity?

 ̄綄美尐妖づ 提交于 2019-12-12 02:45:16

问题


I've got a small piece of code which submits an XML entity along with binary data in the same POST. I'm using the httpclient and httpmime for this.

I'm not quite sure if I should set the Content-Type header for this request. After all, the Content-Type is both application/xml and application/octet-stream.

What's the correct usage for this?

    post = new HttpPost(uri);
    post.setHeader("Authorization", auth);

    // Should I set Content-Type at all?
    post.setHeader("Content-Type", mimeType + ";charset=UTF-8");

    MultipartEntityBuilder b = MultipartEntityBuilder.create();
    b.addTextBody("data", payload, ContentType.APPLICATION_XML);
    b.addBinaryBody("file", file);
    post.setEntity(b.build());

回答1:


No, you should not. You should let HttpClient generate Content-Type as well as other content metadata headers automatically based on properties of the message entity.



来源:https://stackoverflow.com/questions/43510961/should-i-set-the-content-type-header-for-a-multipartentity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!