简体   繁体   English

使用 okHttp 4.* 将 json 数据发布到 rest api 时,响应代码为 400 的错误请求

[英]Bad request with response code 400, when posting json data to rest api using okHttp 4.*

goal : I'm using sharepoint REST APIs to create a list in my sharepoint site.目标:我正在使用 sharepoint REST API 在我的 sharepoint 站点中创建一个列表 In this case I'm using okHttp as my Http library.在这种情况下,我使用 okHttp 作为我的 Http 库。

expected result : should return 201 as response code when calling the request using okHttp.预期结果:使用 okHttp 调用请求时应返回 201 作为响应代码。

actual result :实际结果

Exception in thread "main" java.io.IOException: unexpected code Response{protocol=http/1.1, code=400, message=Bad Request, url=https://***.sharepoint.com/_api/web/lists}
    at com.**.list.ListImpl.createAList(ListImpl.java:39)
    at com.**.Test.main(Test.java:16)

following are the steps I have implemented:以下是我实施的步骤:

  1. create a bearer token创建一个不记名令牌
  2. set the required headers in the request在请求中设置所需的标头
  3. create a object of type which I want to send创建一个我想发送的类型的对象
  4. serialize the object to json, so that data will be send as json data.将对象序列化为 json,以便数据将作为 json 数据发送。

I have used the postman to test the API.我已经使用邮递员来测试 API。 It worked fine , got the desired result.它工作正常,得到了想要的结果。

I have debugged the code, expected values were successfully assigned to the respective variables as well.我已经调试了代码,预期值也成功分配给了相应的变量。

I know that 400 bad request means , there is something wrong with JSON data and server couldn't parse the json which I'm sending.我知道 400 个错误的请求意味着 JSON 数据有问题,服务器无法解析我发送的 json。 Eventhough I tried , but couldn't find the way to resolve this.尽管我尝试过,但找不到解决此问题的方法。 Any help would be appreciated.任何帮助,将不胜感激。


following are the code samples.以下是代码示例。

ListImple.java ListImpl.java

package com.****.list;

import com.**.app.properties.ApplicationProperties;
import com.**.metadata.MetaData;
import com.**.rest.TokenService;
import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

public class ListImpl implements List {

    private String api ="/lists";
    private MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private final OkHttpClient client = new OkHttpClient();

    @Override
    public void createAList(String token) throws IOException {
        MetaData metaData = new MetaData("SP.List");
        ListModel listData = new ListModel(metaData,true,104,
                true,"sample description 02", "list-number-02");

        JSONObject jsonObject = new JSONObject(listData);
        String data = jsonObject.toString();

        RequestBody formBody = RequestBody.create(data, JSON);
        System.out.println(formBody.contentType());

        Request request = new Request.Builder()
                .url(ApplicationProperties.getBaseUrl() + api)
                .addHeader("Authorization", "Bearer " + token)
                .addHeader("Content-Type", "application/json;odata=verbose")
                .addHeader("accept", "application/json;odata=verbose")
                .addHeader("X-RequestDigest", "0x87D8893C3016E8E7EB288E13276DE7C8D5250EC26025FDADCF9A3E6C86DF0C4A5EC86B3B4AEAD882E06058BC0919A61E10C3DEFDE3B275E67698A9E1B4456CEF")
                .post(formBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if(!response.isSuccessful()) {
                throw new IOException("unexpected code " + response);
            }
            System.out.println(response.body().string());
        }
    }

}

After days of debugging I came to conclusion that problem was OkHttp rest library.经过几天的调试,我得出结论,问题是 OkHttp 休息库。 So I removed that library and included the org.apache.httpcomponents artifact in the pom.xml file.所以我删除了那个库并在 pom.xml 文件中包含了org.apache.httpcomponents工件。 now All are working fine.现在一切正常。

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

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