Automated tests 如何在Rest aussured pf RestTemplate中添加标头

Automated tests 如何在Rest aussured pf RestTemplate中添加标头,automated-tests,resttemplate,rest-assured,web-api-testing,Automated Tests,Resttemplate,Rest Assured,Web Api Testing,我一直在尝试使用resttemplate或Restassed库自动化一些API测试,但我在post请求中遇到了问题。我似乎想不出怎么处理这件事。我一直收到415个不支持的类型错误,我已经尝试了很多想法,并阅读了数百条线程。如果有人有解决办法,请告诉我。 这是开发人员代码 @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response postData(@For

我一直在尝试使用resttemplate或Restassed库自动化一些API测试,但我在post请求中遇到了问题。我似乎想不出怎么处理这件事。我一直收到415个不支持的类型错误,我已经尝试了很多想法,并阅读了数百条线程。如果有人有解决办法,请告诉我。 这是开发人员代码

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response postData(@FormDataParam("file") InputStream is,
                         @FormDataParam("file") FormDataContentDisposition fileName,
                         @FormDataParam("checkInCommInfoInput") String checkInCommInfoInput,
                         @HeaderParam("authorization") String authString) { }
这就是我用resttemplate尝试的 字符串addURI=“”; HttpHeaders=新的HttpHeaders()


测试中的API使用(MediaType.MULTIPART\u FORM\u DATA),但测试使用
.header(“内容类型”、“应用程序/json”)发送内容。

415错误正如您明确提到的
不支持的类型错误
在测试客户端发送的主体内容和API接受的内容之间存在内容类型不匹配

请参阅此博客:关于如何发送多部分表单数据

    //headers.add("Accept","*/*");
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", " a values will be here ");



     System.out.println("**************************"+headers.getContentType());

    String jsonBody = "my json file will be here";
    //System.out.println("\n\n" + jsonBody);
    HttpEntity<String> entity = new HttpEntity<String>(jsonBody, headers);

    //POST Method to Add New Employee
    response = this.restTemplate.postForEntity(addURI, entity, String.class);
    responseBodyPOST = response.getBody();
    // Write response to file
    responseBody = response.getBody().toString();
        RestAssured.useRelaxedHTTPSValidation();
    RestAssured.baseURI ="https://myURI";

    EncoderConfig encoderconfig = new EncoderConfig();
     Response response = RestAssured.given()

                .header("Content-Type","application/json" )

                .header("Authorization", "a vvalues will be here")
                .header("Accept",ContentType.JSON )
               .config(RestAssured.config()
                       .encoderConfig(encoderconfig.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
                //.contentType(ContentType.JSON)

               // .accept("application/json")
                .log().all().body(jsonBody).post()
                .then()
                   .assertThat()
                   .log().ifError()
                   .statusCode(200)
                   .extract().response();

     System.out.println("-------------"+ response.getBody().asString());