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

问题描述 投票:0回答:2

放心地得到415错误。相同的URI在浏览器中与Rest客户端一起正常工作。从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());

}
java rest rest-assured
2个回答
0
投票

您使用错误的标题进行接受。 content-type告诉服务器您发送的请求正文;你想使用accept告诉服务器你想要什么类型的响应。


0
投票

需要添加两者

.header("Content-Type","application/json" )
 .header("Accept","application/json" )
© www.soinside.com 2019 - 2024. All rights reserved.