我已将RequestSpecBuilder中的Content-Type设置为“ ContentType.JSON”。但是在发出GET请求时,作为响应,我将Content-Type设为“ application / xml”。我该如何获取JSON响应? 我尝试了以下方法: 1.使用RequestSpecBuilder类的setContentType方法将RequestSpecBuilder对象的内容类型设置为“ ContentType.JSON”,并在RequestSpecification的spec方法中传递RequestSpecBuilder对象---得到“ application / xml”
注意:Web服务URL要求明确指定“ .json”以获得json响应,否则默认情况下它将返回“ xml”响应。但是,我想使用RequestSpecBuilder设置内容类型。 例如: 用于Json响应:URL-http://ergast.com/api/f1/2017/circuits.json Xml响应:URL-http://ergast.com/api/f1/2017/circuits
Code:
@Test
public void test_AddHeader() {
//Use of RequestSpecification
String pathUrl = "http://ergast.com/api/f1/2017/circuits";
RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
requestSpecBuilder = requestSpecBuilder.
setBaseUri(pathUrl).
setContentType(ContentType.JSON).
addQueryParam("limit", "10"); //added query param
RequestSpecification addRequestSpec = requestSpecBuilder.build();
RequestSpecification httpRequest = RestAssured.given().spec(addRequestSpec).contentType(ContentType.JSON);
Response httpResponse = httpRequest.get();
System.out.println(httpResponse.getContentType()); //returns application/xml
System.out.println(httpResponse.getStatusLine()); //returns HTTP/1.1 200 OK
System.out.println(httpResponse.getBody().asString());//returns XML response
}
答案 0 :(得分:0)
您期望响应中有path
,但是您正在将/
传递给<Route component={Layout}>
<Route path="/" component={Home} />
<Route path="/users" component={Users}>
<Route path="user/:id" component={User} />
</Route>
<Route path="/settings" component={Settings} />
<Route path="*" component={NotFound} />
</Route>
。这只会创建json格式的POST负载。它对您的回复没有任何帮助。
您可以做的是创建一个JSON
并在其中执行setContentType
到JSON。希望对您有帮助。