20

我正在尝试使用放心调用休息电话。我的 API 接受,"application/json"作为内容类型,我需要在调用中进行设置。我如下所述设置内容类型。

选项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”(表示“不支持的媒体类型”)。

我尝试使用纯 java 代码调用相同的 api,它可以工作。出于某种神秘的原因,我无法通过 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);
    }
4

8 回答 8

13

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")

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".

于 2016-02-18T12:03:07.060 回答
8

这是使用 CONTENT_TYPE 作为 JSON 的完整 POST 示例。

import io.restassured.http.ContentType;

RequestSpecification request=new RequestSpecBuilder().build();
ResponseSpecification response=new ResponseSpecBuilder().build();
@Test
public void test(){
   User user=new User();
   given()
    .spec(request)
    .contentType(ContentType.JSON)
    .body(user)
    .post(API_ENDPOINT)
    .then()
    .statusCode(200).log().all();
}
于 2017-03-28T06:23:35.913 回答
3

试一下 given().contentType(ContentType.JSON).body(inputPayLoad.toString)

于 2015-08-20T15:45:30.447 回答
1

您的测试可能就是这种情况。尝试这个。

https://github.com/rest-assured/rest-assured/wiki/Usage#avoid-adding-the-charset-to-content-type-header-automatically

避免自动将字符集添加到内容类型标题

默认情况下,REST Assured 会自动添加字符集标头。要完全禁用此功能,您可以像这样配置 EncoderConfig:

RestAssured.config = RestAssured.config(config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));
于 2019-02-28T09:06:09.607 回答
0

我遇到了类似的事情,一段时间后我们注意到问题实际上来自服务器端。请检查您对 Postman 的调用,看看它何时被触发,您是否需要将其从 HTML 更改为 JSON。如果您需要这样做,后端可能需要通过添加其内容类型来强制响应为 JSON格式。即使它是用 JSON 编码的,你仍然可能需要这样做。

这就是我们添加的代码行:

header('Content-type:application/json;charset=utf-8');

.

  public function renderError($err){
   header('Content-type:application/json;charset=utf-8');
   echo json_encode(array(
       'success' => false,
       'err' => $err
   ));
}

这就是后端发生的事情:

在此处输入图像描述

希望能以某种方式提供帮助。:)

于 2018-08-10T14:54:05.777 回答
0

如前文所述,有一个方法:

RequestSpecification.contentType(String value)

我也没有为我工作。但升级到最新版本(此时为 2.9.0)后,它就可以工作了。所以请升级:)

于 2016-11-07T08:14:38.193 回答
0
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import static org.hamcrest.Matchers.is;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;

public class googleMapsGetLocation {
    @Test
    public void getLocation() {
        RestAssured.baseURI = "https://maps.googleapis.com";
        given().param("location", "-33.8670522,151.1957362")
            .param("radius", "500")
            .param("key", "AIzaSyAONLkrlUKcoW-oYeQjUo44y5rpME9DV0k").when()
            .get("/maps/api/place/nearbysearch/json").then().assertThat()
            .statusCode(200).and().contentType(ContentType.JSON)
            .body("results[0].name", is("Sydney"));
    }
}
于 2018-10-29T09:58:11.527 回答
-1

对于您的第一个选项,您能否尝试添加此标头并发送请求?

.header("Accept","application/json")

于 2015-10-30T06:29:52.337 回答