设置内容类型放心 [英] setting content type in rest assured
问题描述
我正在尝试使用放心来调用休息电话。我的API接受application / json
作为内容类型,我需要在通话中设置。我设置了如下所述的内容类型。
I'm trying to invoke a rest call using rest assured. My API accepts, "application/json"
as content type and I need to set in the call. I set the content type as mentioned below.
选项1
Response resp1 = given().log().all().header("Content-Type","application/json")
.body(inputPayLoad).when().post(addUserUrl);
System.out.println("Status code - " +resp1.getStatusCode());
选项2
Response resp1 = given().log().all().contentType("application/json")
.body(inputPayLoad).when().post(addUserUrl);
我得到的回答是415(表示不支持的媒体类型)。
The response I get is "415" (indicates that "Unsupported media type ").
我尝试使用普通的java代码调用相同的api并且它可以工作。出于一些神秘的原因,我不能通过RA工作。
I tried invoking the same api using plain java code and it works. For some mysterious reason, I cudn't get it working through RA.
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(addUserUrl);
StringEntity input = new StringEntity(inputPayLoad);
input.setContentType("application/json");
post.setEntity(input);
HttpResponse response = client.execute(post);
System.out.println(response.getEntity().getContent());
/*
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("Output -- " +line);
}
推荐答案
工作时遇到类似问题有放心的2.7版本。我尝试设置contentType并同时接受application / json,但它不起作用。最后添加了回车符和换行符,如下所示。
I faced similar issue while working with rest-assured 2.7 version. I tried setting both the contentType and also accept to application/json but it didn't work. Adding carriage feed and new line characters at the end as the following worked for me.
RestAssured.given().contentType("application/json\r\n")
添加新行似乎缺少API由于服务器无法区分媒体类型和请求内容的其余部分,因此抛出错误415 - 不支持的媒体类型。
The API seems to be missing to add new line characters after Content-Type header due to which the server is not able to differentiate between the media type and the rest of the request content and hence throwing the error 415 - "Unsupported media type".
这篇关于设置内容类型放心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!