简体   繁体   English

如何使用 RestAssured 发送多部分请求?

[英]How to send a multipart request with RestAssured?

I have @Controller with method with signature like this:我有带有这样签名的方法的@Controller

@PostMapping
@ResponseBody
public ResponseEntity<Result> uploadFileAndReturnJson(@RequestParam("file") MultipartFile file) {}

I want to construct multipart request without physically creating any file.我想在不物理创建任何文件的情况下构建多部分请求。 I tried doing it like this:我试着这样做:

private MultiPartSpecification getMultiPart() {
    return new MultiPartSpecBuilder("111,222")
            .mimeType(MimeTypeUtils.MULTIPART_FORM_DATA.toString())
            .controlName("file")
            .fileName("file")
            .build();
}

Response response = RestAssured.given(this.spec)
            .auth().basic("admin", "admin")
            .multiPart(getMultiPart())
            .when().post(URL);

Unfortunately I received response:不幸的是,我收到了回复:

Required request part 'file' is not present所需的请求部分“文件”不存在

I tried looking at RestAssured unit tests and it seems I'm doing it correctly.我尝试查看 RestAssured 单元测试,看来我做得正确。 If I try to pass byte[] or InputStream instead of String, an exception is thrown:如果我尝试传递 byte[] 或 InputStream 而不是 String,则会引发异常:

Cannot retry request with a non-repeatable request entity.无法使用不可重复的请求实体重试请求。

Thanks for help.感谢帮助。

Your code looks fine and it should work with byte[]. 您的代码看起来不错,它应该与byte []一起使用。 You can use MultiPartSpecBuilder(byte[] content) like below. 您可以使用MultiPartSpecBuilder(byte[] content)如下所示。

private MultiPartSpecification getMultiPart() {
         return new MultiPartSpecBuilder("Test-Content-In-File".getBytes()).
                fileName("book.txt").
                controlName("file").
                mimeType("text/plain").
                build();
   }

The details for error you are getting with byte[] is available at https://github.com/rest-assured/rest-assured/issues/507 . 您可以通过https://github.com/rest-assured/rest-assured/issues/507获取有关byte []的错误的详细信息。 According to this you should try with preemptive basic auth like below. 根据此,您应该尝试使用如下抢占式基本身份验证。

.auth().preemptive.basic("admin", "admin")
try {

    RestAssured.given()
            .header(new Header("content-type", "multipart/form-data"))
            .multiPart("file",new File( "./src/main/resources/test.txt"))
            .formParam("description", "This is my doc")
            .auth().preemptive().basic(loginModel.getUsername(), loginModel.getPassword())
            .when()
            .post(URL)
            .then()
            .assertThat()
            .body(matchesJsonSchemaInClasspath("schemas/members/member-document.json"));
}
catch(Exception e) {
    Assert.assertEquals(false, true);
    logger.error(e.getMessage(), e);
}

I needed to send multiple request with files and json data, i solve it like that我需要发送带有文件和 json 数据的多个请求,我就这样解决了

public static Response Post(JSONObject body, String URL, String file1, String file2) {
        try {
            return RestAssured.given().baseUri(URL).urlEncodingEnabled(false)
                    .accept("application/json, text/plain, */*")
                    .multiPart("data",body,"application/json")
                    .multiPart("file[0]", new File(file1),"multipart/form-data")
                    .multiPart("file[1]", new File(file2),"multipart/form-data")
                    .relaxedHTTPSValidation().when().post();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM