我正在尝试使用HttpClient multipart / form-data创建附件,但不断获取400:Bad请求状态代码作为输出。我在做什么错了。
引荐Apache HttpClient making multipart form post的要求如下:
HttpPost httppost = new HttpPost(URL);
httppost.addHeader("Content-Type", "multipart/form-data");
HttpEntity entity = MultipartEntityBuilder.create()
.addTextBody(name,"{ \"fileName\": \"abc.txt\",
\"description\": { \"raw\": \"Test\" }}" ,
ContentType.create("application/json"))
.addTextBody("file",fileBody_in_bytes,
ContentType.create("text/plain"))
.build();
System.out.print(entity);
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity result = response.getEntity();
httppost.setEntity(result);
But got this error:
{"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:InvalidRequestBody","message":"The request body did not contain the expected multipart parts."}
API中提供的格式如下:
**Header**
Content-Type: multipart/form-data
**Body**
--boundary-delimiter
Content-Disposition: form-data; name="metadata"
Content-Type: application/json; charset=UTF-8
{
"fileName": "cute-cat.png",
"description": {
"raw": "A cute kitty, cuddling with its friends!"
}
}
--boundary-delimiter
Content-Disposition: form-data; name="file"; filename="attachment"
Content-Type: image/png
PNG file data
--boundary-delimiter--
谁能看到我的错误?
谢谢!