[英]Multipart/form-data upload by PUT request, doesn't work on okhttp?
I don't know it is bug, or I am doing something wrong. 我不知道这是错误,还是做错了什么。
Here is my okhttp
request: 这是我的
okhttp
请求:
private static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
protected void putImage(String path) throws IOException {
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"image\""),
RequestBody.create(MEDIA_TYPE_JPEG, new File(path)))
.build();
Request request = new Request.Builder()
.url("http://localhost:9090/")
.put(requestBody)
.build();
Timber.d(request.urlString());
okHttpClient.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Response response) throws IOException {
Timber.d(response.body().string());
}
});
}
It gives error with at the backend code : 后端代码给出错误:
I thought it just backend code has a bug, however, It works on curl
: 我以为只是后端代码有一个错误,但是,它可以在
curl
:
curl -v -XPUT -include --form asdfasdf=@Photo_20140806_000942.jpg http://localhost:9090/
My questions: 我的问题:
putImage
correct and align with okhttp
? putImage
是否正确并与okhttp
对齐? curl
command works very intelligently that make it works? curl
命令是否非常聪明地工作,使其能够工作? okhttp
does extra things that my backend code should handle? okhttp
做后端代码应该处理的其他事情? You might need to specify the filename header. 您可能需要指定文件名标头。 Try setting the
Content-Disposition
like this: 尝试像这样设置
Content-Disposition
:
Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"Photo_20140806_000942.jpg\"")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.