即使存在“ Content-Type”,也获得了415(不受支持的媒体类型)

时间:2018-06-28 01:16:55

标签: java json okhttp3 rest-assured http-status-code-405

我使用了okhttp v3.10.0。 即使请求中存在“ Content-Type”,我也收到415错误。 (当我使用okhttp和RestAssured库时,结果是相同的。) 我的标题之一是“ Content-Type:application / json”。 但这可以通过使用邮递员工具来实现。

下面是我的pom.xml。

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

<groupId>JavaTest</groupId>
<artifactId>com.test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
                <argLine>-Dfile.encoding=UTF-8</argLine>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.10.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.daisy.pipeline.modules/mediatype-utils -->
    <dependency>
        <groupId>org.daisy.pipeline.modules</groupId>
        <artifactId>mediatype-utils</artifactId>
        <version>1.0.4</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

下面是我的代码。请让我从这个地狱出来!!!

import okhttp3.*;
import org.junit.Test;

public class JavaTest {
    @Test
    public void testJava() {
        try {
            OkHttpClient client = new OkHttpClient();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "{\n    \"ListStoreInfo\": [\n        \"APS\",\n        \"GGL\"\n    ],\n    \"ListInappInfo\": [\n        {\n            \"StoreCd\": \"APS\",\n            ...}");
            Request request = new Request.Builder()
                    .url("http://test-myurl/Product/Modify")
                    .put(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "*/*")
                    .addHeader("Cache-Control", "no-cache")
                    .addHeader("Accept-Encoding", "gzip, deflate")
                    .addHeader("Origin","chrome-extension//fhbjgbiflinjbdggehcddcbncdddomop")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("Host", "test-myurl.com")
                    .build();
            Response response = client.newCall(request).execute();
            System.out.println("***********************************************************************************");
            System.out.println(request.url().toString());
            System.out.println(request.headers().toString());
            System.out.println(request.body());
            System.out.println("***********************************************************************************");
            System.out.println(response.message());
            System.out.println(response.body().string());
            System.out.println("***********************************************************************************");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

enter image description here

0 个答案:

没有答案