小编Tec*_*kie的帖子

设置内容类型放心

我正试图用放心的方式调用休息电话.我的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());
Run Code Online (Sandbox Code Playgroud)

选项2

Response resp1 = given().log().all().contentType("application/json")
   .body(inputPayLoad).when().post(addUserUrl);
Run Code Online (Sandbox Code Playgroud)

我得到的响应是"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);
    }
Run Code Online (Sandbox Code Playgroud)

java rest-assured

11
推荐指数
3
解决办法
2万
查看次数

标签 统计

java ×1

rest-assured ×1