Apache Http 4.5 StringBody构造函数不在请求中导出内容类型

时间:2016-02-27 21:29:45

标签: docusignapi apache-httpclient-4.x

使用Apache Http 4.5 MultipartEntityBuilder似乎无法弄清楚为什么StringBody(String,ContentType)构造函数实际上没有在请求表单体中输出Content-Type。

public HttpRequestBase build() throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost("https://{server}/restapi/{apiVersion}/accounts/{accountId}/envelopes");

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setContentType(ContentType.MULTIPART_FORM_DATA);
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.setBoundary("AAA");

    //add form body
    builder.addPart(generateJsonFormBodyPart());

    //add file body
    builder.addPart(generateFileFormBodyPart()); //<--intentionally omitted

    HttpEntity multipart = builder.build();

    httpPost.setEntity(multipart);

    return httpPost;

}

private FormBodyPart generateJsonFormBodyPart() throws UnsupportedEncodingException{
        StringBody json = new StringBody(packageJson(), ContentType.APPLICATION_JSON); //<--THIS DOESN'T SEEM TO WORK    
        StringBuilder buffer = new StringBuilder();
        buffer.append("form-data");

        String contentDisposition = buffer.toString();

        FormBodyPartBuilder partBuilder = FormBodyPartBuilder.create("application/json", json);
        partBuilder.setField(MIME.CONTENT_DISPOSITION, contentDisposition);

        FormBodyPart fbp = partBuilder.build();

        return fbp;
    }

文件部分输出正常,但我从对等体返回“错误请求”,我认为这是因为它有非常具体的请求参数。

必需的请求输出

接受:application / json 内容类型:multipart / form-data;边界= AAA

- AAA Content-Type:application / json 内容处理:表单数据

json已删除

- AAA 内容类型:application / pdf 内容处理:文件;文件名=则为 “test1.pdf”; documentid = 1

删除了文档

实际Apache Http 4.5输出

X-Docusign-Act-As-User:xyz@company.com Accept-Encoding:gzip,deflate User-Agent:Apache-HttpClient / 4.5(Java / 1.8.0_65) 连接时间:0 主持人:requestb.in 连接:关闭 内容长度:3178 授权:bearer xxxxrandomoauthtokenxxxxx 内容类型:multipart / form-data;边界= AAA;字符集= ISO-8859-1 通过:1.1 vegur X-Request-Id:89cd1cf5-3615-41e8-84ba-cd076a03af67 总路线时间:0

- AAA 内容处理:表单数据 //&lt; - 问题。应该是application / json no?

{“status”:“created”,“emailBlurb”:“Welcome to Confluence”,“emailSubject”:“Welcome to Confluence”,“documents”:{“name”:“Welcome to Confluence.html”,“ documentId “:” 1" , “订单”: “1”}, “接收者”:{}}

- AAA 内容处理:文件;文件名= “Welcome.html”; documentid = 1 内容类型:text / html;字符集= ISO-8859-1

删除字符串

问:那么为什么StringBody构造函数中的ContentType会被忽略?有解决方法还是我做错了?

1 个答案:

答案 0 :(得分:0)

在处理StringBody时,我确信这是Apache Http Mime中FormBodyPart.build()的一个错误。从4.5.2的发行说明中的​​证据表明,对于未输出“Content-Type”的其他“body”类型的错误修复,支持这一点。我将记录缺陷。

解决方法:

private FormBodyPart generateJsonFormBodyPart() throws UnsupportedEncodingException{
    StringBody json = new StringBody(getMyJsonStuff(), ContentType.APPLICATION_JSON); //<--THE GOGGLES, THEY DO NOTHING!!

    StringBuilder buffer = new StringBuilder();
    buffer.append("form-data");
    buffer.append("\r\n");
    buffer.append("Content-Type: application/json"); //<--tack this on to the 

    String kludgeForDispositionAndContentType = buffer.toString();

    FormBodyPartBuilder partBuilder = FormBodyPartBuilder.create("stuff", json);
    partBuilder.setField(MIME.CONTENT_DISPOSITION, kludgeForDispositionAndContentType);

    FormBodyPart fbp = partBuilder.build();

    return fbp;
}

像魅力一样。