【问题标题】:Getting status code 415 for Rest API URI Via Rest Assured but working fine via Rest Client通过 Rest Assured 获取 Rest API URI 的状态代码 415,但通过 Rest Client 工作正常
【发布时间】:2018-03-01 13:19:55
【问题描述】:

放心得到 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());

}

【问题讨论】:

  • 我在请求中添加了这个,现在它工作正常。 contentType("应用程序/json")

标签: java rest rest-assured


【解决方案1】:

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

【讨论】:

  • 我也试过 "accept" "application/json" 但得到相同的结果
  • 你是否也删除了坏的content-type
  • 是的,我删除了内容类型
  • 如果您仍然无法让它与浏览器响应匹配,您应该尝试发送与浏览器发送相同的标头。
【解决方案2】:

两个都需要加

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    在我的情况下,问题是由放心引起的,它将“charset”附加到请求标头"content-type=application/json"。 当服务器有原则并严格遵守 RFC 7159 时,此类请求头可能会被拒绝

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多