我正在尝试通过我的 android 应用程序制作HttpPost
一个multiPart/form-data
。我有一个使用我的 api 的邮递员测试,并且在邮递员中预览该请求,如下所示:
POST /api/0.1/content/upload HTTP/1.1
Host: 54.221.194.167
X-AUTHORIZATION: 166e649911ff424eb14446cf398bd7d6
Cache-Control: no-cache
Postman-Token: 2412eba9-f72d-6f3b-b124-6070b5b26644
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"
{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"; filename="addedaslib.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
我正在尝试将 multipart/form-data 与我的 android HttpPost 一起使用,但它似乎不起作用。有没有办法“预览”我的请求并查看它实际上是如何发布到 api 的?我究竟做错了什么?我的代码:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {
client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response = null;
MultipartEntity mpe = new MultipartEntity();
try {
Log.v("API", "URL:"+url);
request.setHeader("Content-Type", "multipart/form-data");
request.addHeader("X-AUTHORIZATION",token);
request.addHeader("Cache-Control", "no-cache");
DRPContentForUpload content = new DRPContentForUpload(file);
String jsonObject = DRPJSONConverter.toJson(content);
FormBodyPart part1= new FormBodyPart("file01", new StringBody(jsonObject));
FormBodyPart part2= new FormBodyPart("file01", new FileBody(file));
mpe.addPart(part1);
mpe.addPart(part2);
//
request.setEntity(mpe);
Log.v("RAW REQUEST", "request looks like:"+mpe.toString());
response = client.execute(request);
编辑
我能够与我的 API 团队交谈,他们说我的帖子实际上是这样的:
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit
{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"; filename="IMG_20140131_111622.jpg"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
那就是说它仍然无法正常工作并吐出我缺少参数的错误
这是我项目中包含的库的屏幕截图: