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
  • 12,336
  • 15
  • 73
  • 115
  • Can you be a bit more specific on what is not working? Are you getting any specific error or anything else that you can share with us? – Valentin Despa Jun 14 '17 at 21:39
  • @ValentinDespa It's not much want I want to do. I just want to post a file and a json model with one request. The error I got is in my question. If I create a sftpModel.json file instead of write the model itself, it works – Patrick Jun 19 '17 at 14:01
  • @Patrick were you able to find a solution for this? I am getting the same problem. – NewQueries Nov 28 '17 at 03:43
  • 1
    @NewQueries no. I just got it working by sending the file via multipart and the json as request parameter. – Patrick Nov 28 '17 at 07:25
  • Have a look at this answer https://stackoverflow.com/a/16022213/355438 – Ilya Serbis Dec 19 '17 at 10:05

2 Answers2

2

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
  • 21
  • 3
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,392
  • 10
  • 19