Post file using webclient webflux to API accepting APPLICATION_OCTET_STREAM

Viewed 2746

I want to upload file to rest api using Webflux web client. the API which accepts the file has content type as APPLICATION_OCTET_STREAM how i can do this using spring WebClient?

how can i convert below curl to WebClient request ?

curl -i -X POST --header "Content-Type:application/octet-stream" --header --data-binary @myfile.pdf  https://some-host/some-url

Note - I am not expecting very large file

1 Answers

This is how i was able to upload file

Step 1 : Read file as bytes array

bytes = new FileSystemResource((filePath)).getInputStream()
                    .readAllBytes();

Step 2 : pass the byte array to bodyValue method

webClient.post()
         .uri("Some-uri)
         .contentType(MediaType.APPLICATION_OCTET_STREAM)
         .bodyValue(bytes)
         .retrieve()
         .toBodilessEntity()) 
Related