MultipartBody请求POST不适用于OKHTTP

时间:2019-07-06 05:21:17

标签: android django-rest-framework okhttp3

我有两个类似的请求正文:

body 帖子没有问题:

public class prop0 {
    public int prop_post;
    public String prop_post_owner;

    public prop0(Integer id) {
        this.prop_post = id;
        this.prop_post_owner = "";
    }
}
prop0 obj = new prop0(id);
RequestBody body = RequestBody.create(JSON, new Gson().toJson(obj));

但是这给了我错误的请求(400)错误:

RequestBody body2 = new MultipartBody.Builder()
                          .setType(MultipartBody.FORM)
                          .addFormDataPart("prop-post", Integer.toString(id))
                          .addFormDataPart("prop_post_owner","").build();

这是我同时发送两个请求:

Request post_request = new Request.Builder()
                                        .url(root + "images/")
                                        .addHeader("Authorization", header2)
                                        .post(body2)
                                        .build();

OkHttpClient client = new OkHttpClient();
Response response2 = client.newCall(post_request).execute();

我的后端是Django Rest框架。

有人可以让我知道okhttp的multipartrequest有何特别之处吗?我需要使用它发布图像,所以我很难弄清楚为什么它不起作用。预先感谢

0 个答案:

没有答案