0

I want call controller method with Multipart file and json as parameters for it.

Here is code how I've called with JSON only(successfully):

@CrossOrigin
@RequestMapping(
        value = "/login",
        method = POST,
        produces = APPLICATION_JSON_VALUE,
        consumes = {APPLICATION_JSON_VALUE}
)
public JsonUser login(@RequestBody LoginRequest loginRequest,
                  BindingResult bindingResult,
                  HttpServletResponse response,
                  HttpServletRequest request) throws Exception {

///CODE HERE
    return JsonUser.from(userContext);
}

Here is LoginRequest is class-model for my json request:

@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginRequest {
private String email;

private String password;

public String getEmail() {
    return email;
}


public String getPassword() {
    return password;
}


}

Now I want make request with Image and json above in one REST controller's method. My code:

 @RequestMapping( method = RequestMethod.POST,
        value = "/create_ticket")
public JsonUser scan(@RequestParam("file") @Valid @NotBlank MultipartFile file,  RegistrationRequest test) {

    //CODE HERE
}

But i have no success. I can give MultipartFile if it's alone and json also. But i can't together.

Here is postman-data i send enter image description here

enter image description here

artemk
  • 121
  • 4
  • 12
  • it looks like youre trying to specify 2 different request bodies, why do you want them in the same request? – mast3rd3mon May 08 '18 at 10:38
  • I want send image and some data(name, description, etc) in one request. I've decided send json with data(name,desc,etc) as one object and picture as other – artemk May 08 '18 at 10:41
  • why not just turn the image to a base64 string and pass it with the json? – mast3rd3mon May 08 '18 at 10:42
  • That's an idea, can you provide info how to do this with postman? Then i will point your answer as correct – artemk May 08 '18 at 10:47
  • And one more question. In Json-class model i would have image as string alsO? so i need to convert it back to Multipart file? – artemk May 08 '18 at 10:48
  • you cant base64 an image in postman (to my knowledge) but there will be some online base64 encoders that you can use to get a base64 string. Then just paste the string into the postman object – mast3rd3mon May 08 '18 at 10:48
  • that's strange solution for me – artemk May 08 '18 at 11:02
  • 1
    What you try to do is not really possible, here is why https://stackoverflow.com/questions/23118249/whats-the-difference-between-request-payload-vs-form-data-as-seen-in-chrome – Oleg Sklyar May 08 '18 at 11:02
  • Are you sure of this? There is enctype "multipart/mixed" as i understand this type for my situation – artemk May 08 '18 at 11:14

0 Answers0