简体   繁体   English

通过 Rest Assured 获取 Rest API URI 的状态代码 415 但通过 Rest Client 工作正常

[英]Getting status code 415 for Rest API URI Via Rest Assured but working fine via Rest Client

Getting 415 error in rest assured.放心得到415错误。 Same URI is working fine with Rest client in browser.相同的 URI 在浏览器中与 Rest 客户端一起工作正常。 From Rest client i am getting response code 200.从 Rest 客户端,我收到响应代码 200。

public static Response response;
public static String jsonAsString;


@BeforeClass
public static void setupURL()
{
    // here we setup the default URL and API base path to use throughout the tests
    RestAssured.baseURI = "######url##########";
    //RestAssured.basePath = "/api/v1";
    RestAssured.authentication = basic("password", "password");
    Header acceptJson = new Header("content-type", "application/json");
    //RestAssured.given().contentType(ContentType.JSON);
    RestAssured.given().header(acceptJson);
}

@Test
public void getImageThroughImageid(){
    RequestSpecification httpRequest = RestAssured.given();
    Response response = httpRequest.request(Method.GET, "/images/imageid");
    System.out.println(response.getStatusCode());

}

You're using the wrong header for your accept.您为接受使用了错误的标题。 content-type tells the server what request body you're sending; content-type告诉服务器你发送的是什么请求体; you want to use accept to tell the server what type of response you want.您想使用accept来告诉服务器您想要什么类型的响应。

Need to add Both两个都需要加

.header("Content-Type","application/json" )
 .header("Accept","application/json" )

Try this:试试这个:

EncoderConfig encoderConfig = RestAssured.config().getEncoderConfig()
                      .appendDefaultContentCharsetToContentTypeIfUndefined(false);

RestAssured.config = RestAssured.config().encoderConfig(encoderConfig);

In my case problem was caused by rest assured, which appends "charset" to request header "content-type=application/json" .在我的情况下,问题是由放心引起的,它将 "charset" 附加到请求标头"content-type=application/json" Such request header may be refused when the server is principled and strict comply RFC 7159当服务器原则性和严格遵守 RFC 7159 时,此类请求头可能会被拒绝

Simply update rest-assured dependency & try to post a payload.只需更新放心依赖并尝试发布有效负载。

Older rest-assured library appends charset after 'content-type' even if you explicitly set it "application/json".较旧的放心库会在“内容类型”之后附加字符集,即使您明确将其设置为“application/json”也是如此。

Headers:        Accept=*/*
                Authorization=Bearer --token**
                Content-Type=application/json; charset=UTF-8

but new rest-assured library will furnish header this way..但是新的放心图书馆将以这种方式提供标题..

Headers: Accept= / Authorization=Bearer --token** Content-Type=application/json标头:Accept= / Authorization=Bearer --token** Content-Type=application/json

I updated below dependency which resolved my issue.我更新了以下依赖关系,解决了我的问题。

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
        <scope>test</scope>
    </dependency>

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

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