3

I want to send a file and a json model at one post request.

My Request Mapping looks like that:

@ResponseBody
@RequestMapping(value = "/sftp/upload", method = RequestMethod.POST)
public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) {

My Json has this structure:

{
  "sftpHost": "ftp01.Host.de",
  "sftpPort": 22,
  "sftpUser": "anyUser",
  "sftpPassword": "anyPass",
  "sftpRemoteDirectory": "/"
}

And the file is on my system.

I'm able to send the file or the sftpModel seperatly but not together. The error I receive is:

{
  "timestamp": 1497336812907,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'application/octet-stream' not supported",
  "path": "/secure-data-transfer-service/sftp/upload"
}

I tried it with postman and curl. But no chance.enter image description here

curl --form "file=@test.txt" --form "sftpModel={"sftpHost":"ftp01.Host.de","sftpPort":22,"sftpUser":"anyUser","sftpPassword":"anyPass","sftpRemoteDirectory":"/"}" http://localhost:8080/secure-data-transfer-service/sftp/upload

Is there any way to send both?

Patrick
  • 11,357
  • 14
  • 68
  • 108

2 Answers2

1

You java code is looks like perfect.

@ResponseBody @RequestMapping(value = "/sftp/upload", method = RequestMethod.POST) public ResponseEntity<SftpModel> upload(@RequestPart("file") MultipartFile file, @RequestPart("sftpModel") SftpModel sftpModel) { }

You can write your SftpModel json string in one json file and try uploading with that json file.

Click here to see the postman image

ankitS
  • 11
  • 2
0

Please try with below code:

public ResponseEntity<?> uploadFile(@RequestPart MultipartFile file, @RequestPart String user) {
            User users = new ObjectMapper().readValue(user, User.class);
}
Jay Thakkar
  • 1,274
  • 9
  • 18