Using Spring WebClient to upload a file in java

2 answers

I am trying to migrate from normal HttpPost methods to Spring WebClient and I have an API that accepts two files (one JSON and one PDF) for upload.

I am trying to send the files like below, but getting a 500 Internal Server Error instead of the 200 OK.

String jsonData =""; ByteArrayOutputStream file;  MultipartBodyBuilder builder = new MultipartBodyBuilder(); String header1 = String.format("form-data; name=%s; filename=%s", "attach", "file.pdf"); String header2 = String.format("form-data; name=%s; filename=%s", "jsonfile", "jsonfile.json");  // This line is causing the problem, Am I making a mistake here? builder.part("attach", file.toByteArray()).header("Content-Disposition", header1); // This line works fine. builder.part("jsonfile", jsonData.getBytes()).header("Content-Disposition", header2);  WebClient webClient = WebClient.create("a url");          byte[] fileContent = null;         try {             fileContent = webClient.post()                 .body(BodyInserters.fromMultipartData(builder.build()))                 .retrieve()                 .onStatus(HttpStatus::isError, res -> handleError(res))                 .bodyToMono(byte[].class)                 .block();         } catch (Exception e) {             return null;         } 

However, If I do not send the PDF file in the request, it works fine with only the JSON file. With the Postman both cases work fine.

I assume that I am making a mistake when adding the PDF file to the request. Though the file itself is a valid PDF, and the response of the API is a JSON file.

If someone could tell me what could possibly be wrong here.

All answers to this question, which has the identifier 61229388

The best answer:

Maybe if you put "file.pdf" in line 5 into quotes solve your problem

After various changes, I was able to fix this issue. It might come handy for someone who stumbles into this issue.

Instead of using file.toByteArray() directly, use new ByteArrayResource(file.toByteArray()) instead.

So the line looks like:

builder.part("attach", new ByteArrayResource(file.toByteArray())).header("Content-Disposition", header1); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data