确保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());
}
答案 0 :(得分:0)
您使用错误的标题进行接受。 content-type
告诉服务器您要发送的请求正文;您想使用accept
告诉服务器您想要什么类型的响应。
答案 1 :(得分:0)
需要同时添加
.header("Content-Type","application/json" )
.header("Accept","application/json" )
答案 2 :(得分:0)
尝试一下:
EncoderConfig encoderConfig = RestAssured.config().getEncoderConfig()
.appendDefaultContentCharsetToContentTypeIfUndefined(false);
RestAssured.config = RestAssured.config().encoderConfig(encoderConfig);
就我而言,问题是由放心造成的,它在请求标头"content-type=application/json"
后面附加了“字符集”。
当服务器具有原则性并严格遵守RFC 7159时,可能会拒绝此类请求标头