设置内容类型放心

时间:2014-11-17 15:57:31

标签: java rest-assured

我试图用放心的方式调用休息电话。我的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);
    }

8 个答案:

答案 0 :(得分:9)

我在使用可靠的2.7版本时遇到了类似的问题。我尝试设置contentType并同时接受application / json,但它没有工作。最后添加了回车符和换行符,如下所示。

RestAssured.given().contentType("application/json\r\n")

在Content-Type标头之后添加新行字符似乎缺少API,因为服务器无法区分媒体类型和其他请求内容,因此抛出错误415 - &#34 ;不支持的媒体类型"。

答案 1 :(得分:1)

试一试 给定的()。contentType中(ContentType.JSON)。体(inputPayLoad.toString)

答案 2 :(得分:1)

以下是使用CONTENT_TYPE作为JSON的完整POST例子。希望它可以帮到你。

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();
}

答案 3 :(得分:0)

对于您的第一个选项,您可以尝试添加此标题并发送请求吗?

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

答案 4 :(得分:0)

如前所述,有一种方法:

RequestSpecification.contentType(String value)

我也没有为我工作。但升级到最新版本(在这一刻2.9.0)之后就可以了。所以请升级:)

答案 5 :(得分: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
   ));
}

这就是后端发生的事情:

enter image description here

希望可以有所帮助。 :)

答案 6 :(得分: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"));
    }
}

答案 7 :(得分:0)

相关问题