Ответ OkHttp 204 с типом содержимого json

Когда я использую okhttp3 версии 4.7.2 для создания запроса json с третьим API, я получаю ответ 204. Но когда я использую asyncHttpClient для создания того же запроса json, я получаю правильные данные.

Это мой код, использующий okhttp3:

        MediaType JSON =  MediaType.get("application/json");

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(interceptor).build();

        RequestBody body = RequestBody.create("{}", JSON);
        Request request = new Request.Builder()
                .url(URL).post(body).build();
        System.out.println("Start");
        okhttp3.Response response = client.newCall(request).execute();
        System.out.println(response.body().string()); // null
        System.out.println(response.code()); // 204

Это мой код с использованием asyncHttpClient:

        ListenableFuture<org.asynchttpclient.Response> whenResponse = client
                .preparePost(URL)
                .addHeader("Content-Type", "application/json").setBody("{}").execute();
        org.asynchttpclient.Response response = whenResponse.get();
        System.out.println(response.getResponseBody()); // Correct response body
        System.out.println(response.getStatusCode()); // 200

person Xperia    schedule 17.09.2020    source источник


Ответы (1)


Запрос json, отправленный okhttp, имеет тип содержимого application/json; charset=utf-8, третий API разрешает только application/json. Итак, их ответ 204.

person Xperia    schedule 18.09.2020