How to send object using BodyInserters.fromFormData
Matthew Harrington
I'm currently asking you a question because I'm using a spring to write a test code that I don't know how to solve.
The problem i'm trying to solve now is that i don't know how to send DTO to send Form Post Function using BodyInserters.
The codes below are my DTO and test codes.
StudentInfo studentInfo = StudentInfo.builder() .studentId(2014l) .name("s1") .email("[email protected]") .phoneNumber("1234") .universityCode("cbnu") .build();
ContestInfo contestInfo = ContestInfo.builder() .name("contest 1") .description("easy contest") .activeTime(LocalDateTime.now()) .inActiveTime(LocalDateTime.now()) .startTime(LocalDateTime.now()) .endTime(LocalDateTime.now()) .freezeTime(LocalDateTime.now()) .unFreezeTime(LocalDateTime.now()) .build();
WebTestClient.ResponseSpec responseSpec = webTestClient.post() .uri("/questions") .contentType(MediaType.MULTIPART_FORM_DATA) .body(BodyInserters.fromFormData("contest", AcceptanceTestUtils.ConvertObjectToMap(contestInfo).toString()) .with("student", AcceptanceTestUtils.ConvertObjectToMap(studentInfo).toString()) .with("activeTime", String.valueOf(LocalDateTime.now())) .with("problemNumber", String.valueOf(1L)) .with("content", "test") .with("response", " ") .with("createTime", String.valueOf(LocalDateTime.now()))) .exchange() .expectStatus() .isCreated() .expectHeader().valueMatches("location", "/questions/[1-9]+[0-9]*");Especially among the parts of the test code.
.body ("contest", AcceptanceTestUtils.ConvertObjectToMap").toString()
.with ("student", AcceptanceTestUtils.ConvertObjectToMap).toString()
These are the two lines that I've been thinking about.
I thought I could convert the DTO into a map and deliver the string again, but there is a problem that keeps not being delivered.
contest=%7BactiveTime%3D2020-05-20T12%3A31%3A01.718%2C+name%3Dcontest+1%2C+description%3Deasy+contest%2C+startTime%3D2020-05-20T12%3A31%3A01.719%2C+freezeTime%3D2020-05-20T12%3A31%3A01.719%2C+endTime%3D2020-05-20T12%3A31%3A01.719%2C+unFreezeTime%3D2020-05-20T12%3A31%3A01.719%2C+inActiveTime%3D2020-05-20T12%3A31%3A01.719%7D&student=%7BstudentId%3D2014%2C+phoneNumber%3D1234%2C+name%3Ds1%2C+universityCode%3Dcbnu%2C+email%3Da%40b.c%7D&activeTime=2020-05-20T12%3A31%3A01.749&problemNumber=1&content=test&response=+&createTime=2020-05-20T12%3A31%3A01.749The values transferred are as above. How can I fix this?
1 Related questions 2 How to insert request body using BodyInserters in Spring5? 30 How to send request body in spring-boot web client? 1 How to get the request data body through ServerWebExchange? Related questions 2 How to insert request body using BodyInserters in Spring5? 30 How to send request body in spring-boot web client? 1 How to get the request data body through ServerWebExchange? 2 Webclient send POST without body will get IllegalStateException 7 Spring WebClient Post method Body 2 Trying to Send list of string in post request using webClient in spring 3 Spring WebClient Post body not getting passed 0 Using @RequestBody with wrapper object 5 BodyInserters.fromObject is deprecated, what is the alternative? 0 Using BodyInserters to pass parameter with webClient Load 7 more related questions Show fewer related questions Reset to default