0

I'm trying to send a file to a spring-boot-starter-webflux application.

This is my controller:

@RestController
@RequestMapping("/fplan")
public class FplanController {
  
    private final FplanService fplanService;

    public FplanController(FplanService fplanService) {
        this.fplanService = fplanService;
    }

    @PostMapping(value = "/file")
    public Flux<Boolean> handleFileUpload(@RequestPart("file") MultipartFile file) throws IOException {
        LOGGER.info(file.getOriginalFilename());
        return fplanService.newFplan(file.getInputStream());
    }
}

And this is my curl command line:

curl -v -F 'file=@fplan.txt' 'http://localhost:8082/fplan/file'

And this is the error output:

*   Trying 127.0.0.1:8082...
* Connected to localhost (127.0.0.1) port 8082 (#0)
> POST /fplan/file HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Length: 16985001
> Content-Type: multipart/form-data; boundary=------------------------76d46a224dfc0ceb
> Expect: 100-continue
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 415 Unsupported Media Type
< Content-Type: application/json
< Content-Length: 137
<
{"timestamp":"2022-03-07T15:30:53.056+00:00","path":"/fplan/file","status":415,"error":"Unsupported Media Type","requestId":"936a38c5-5"}

I already tried:

@PostMapping(value = "/file", consumes = MediaType.ALL_VALUE)

or

@PostMapping(value = "/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

or

curl -v -X POST -H "Content-Type: multipart/form-data" -F 'file=@./fplandb_fv.txt;type=text/plain' "http://localhost:8082/fplan/file"

without success.

Any ideas whats missing?

BetaRide
  • 16,207
  • 29
  • 99
  • 177

1 Answers1

0

After all I found this post POST binary file with cmd line curl using headers contained in the file.

If I change the controller method like this

@PostMapping(value = "/file", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public Flux<Boolean> handleFileUpload(@RequestBody byte[] bytes) throws IOException {
    LOGGER.info("Received {} bytes", bytes.length);
    return fplanService.newFplan(new ByteArrayInputStream(bytes));
}

then this curl line sends the file:

curl -v --header "Content-Type:application/octet-stream" --data-binary @fplandb_fv.txt 'http://localhost:8082/fplan/file'

Feel free to post an answer that explains how to send a file for the controller implementation in the question.

BetaRide
  • 16,207
  • 29
  • 99
  • 177