简体   繁体   English

表单数据请求,使用 okHttp3

[英]form-data request, with okHttp3

I'm trying to make a request where I pass some parameters in the Body, as shown in the image.我正在尝试发出一个请求,在其中我在正文中传递了一些参数,如图所示。

Example Image示例图像

Example:例子:

Key: file[], Value: "xml", Content-Type: application/xml Key: query, Value: {"boxe/File": false}, Content-Type: application/xml

I'm getting a Bad Request error, I think my code isn't right.我收到Bad Request错误,我认为我的代码不正确。 Follow how it is being done关注它是如何完成的

RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("file[]", xml, RequestBody.create(null, "application/xml"))
        .addFormDataPart("query", "{\"boxe/File\": false}", RequestBody.create(null, "application/xml"))
        .build();

Request request = new Request.Builder().url(endPoint).addHeader("x-integration-key", integrationKey)
        .addHeader("Authorization", "Bearer " + token)
        .post(requestBody).build();

Managed to solve it, the order of the parameters were wrong,好不容易解决了,参数顺序错了,

follow the correct order遵循正确的顺序

  RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file[]","", RequestBody.create(MediaType.parse("application/xml"), xml))
            .addFormDataPart("query", "", RequestBody.create(MediaType.parse("application/json"), boxFileJson))
            .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM