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

标签 java rest rest-assured

安心拿到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());

}

最佳答案

需要同时添加

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

关于java - 通过 Rest Assured 获取 Rest API URI 的状态代码 415 但通过 Rest Client 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49050592/

相关文章:

java - Java 中的 securitypack.jar 是什么?

java - 如何让倒计时进度条不断向下?

rest - 一旦集群 IP 及其端口出现故障,就会出现问题

java - 使用 Rest-Assured 和 java 从 JSON 响应获取特定数据

java - 结果集不包含新插入的行

Java - 多线程服务器为多个并发客户端提供服务

java - 放心地从未命名的根查找对象

java - 无法在请求中设置 cookie

javascript - 如何让 Angular 前端等待快速 REST-Call 响应

python - 基本 Flask 应用程序未运行(类型错误 : required field "type_ignores" missing from Module)