Spring Rest - Post multiple files - java

Using Spring Boot 2.1.8, I have two methods in a Rest Controller that expect a single file and multiple files respectively. These are the method signatures:
#PreAuthorize("hasAnyRole('ROLE_ADMIN')")
#PostMapping("/uploadMultipleFiles")
public List<FileResponse> uploadMultipleFiles(#RequestParam("files") MultipartFile[] files);
#PreAuthorize("hasAnyRole('ROLE_ADMIN')")
#PostMapping("/upload")
public FileResponse uploadFile(#RequestParam("file") MultipartFile file);
The single upload works just perfect. I am managing to load a single file from a web client, Postman v7.25.0, and from Swagger 2.
But the multiple file method only works when uploading files from Postman, returning a 400 http error code. Therefore, it does not even enter the method.
The error message in both cases (web client or Swagger) is the same:
can't parse JSON. Raw result:
Missing or unreadable multipart file in request
This is the request headers when calling to /uploadMultipleFiles through Swagger (getting error):
This is the request headers when calling /uploadMultipleFiles from Postman (works fine):
This is the request headers when calling /upload from Postman (works fine):
This is the request headers when calling /upload through Swagger (works fine):
Firstly I thought that Content-Type might have something to do with my problem. But Swagger sends always application/json, and it works with the single upload endpoint.
Any idea?

When you are dealing with file uploads using multipartfile, you should set the content type of the request to multipart/form-data. The screenshot shows that Postman is using multipart/form-data and not application/json.

Related

using RestTemplate.exchange GET request vs posting URL in google chrome

Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods.
Chrome (or any browser) will send several extra HTTP headers along with the request, such as user agent, cookies, etc, by default. It'll also render any Javascript from the response as well as make extra requests for static assets such as CSS files, fonts, images, etc. Unclear what you mean by "url posting", but POST requests are typically only done on HTML forms.
RESTTemplate will only send headers and session data you've explicitly set, and will not evaluate any Javascript content in responses. It'll return the plaintext returned from the web server, and not request files linked within an HTML response.
An "image response" would be returned as bytes for both, potentially with a Content-Type header. RESTTemplate cannot "render" HTML <img> tags. Instead, you'd need to parse the src address, then make a new GET request for static assets

Photo Upload - Jmeter - {"reason":"Chunk-Length mismatch (found: 73800, required:73581)"}

I am trying to upload photo via Jmeter, below is the sample request:
URL: https://domain/.../signedresources/604a9230-..../resumable?region=US
Method: PUT
Headers:
Content-Type - image/jpeg, image/jpeg
Session-Id - 884b03...
Content-Range - bytes 0-73580/73581
Content-Length - 73581
In File Upload tab of Jmeter I have checked: Use Multipart/form-data option
File Path: /Users/sujata.../Downloads/fish.jpeg
Parameter Name: file_upload[]
MIME type: image/jpeg
Now, this request works fine when executed via Postman, and in Postman Content-Length 73581 header is not passed. The file size in bytes is 73581. However while running this request via jmeter Content-Length - 73581 header is automatically added and everytime the value is different, which is ending up with below error:
{"reason":"Chunk-Length mismatch (found: 73800, required:73581)"}
Is there any way to compute the content-length of file which we are going to upload and pass that value in the header?
I also tried explicitely mentioning Content-Length - 73581 in header but still it is not taking this valued and somehow some new value is computed and mismatch error is shown.
Is there any way to bypass this content-length computation by jmeter, or overwrite?
PUT-Request-Jmeter
Content-Type_header_added_by_jmeter
Content-Length header stands for the number of bytes in the request body. The request body begins right after the Headers. So if you're uploading just one file and there are no dynamic parts of the request - it should be the same every time you execute the request. However for multipart requests you have the boundary which is random and its length may vary, that explains the eventual differences.
It might be the case Postman sends the file in "raw" mode, i.e. not multipart. In this case you will need to untick Use multipart/form-data box in the HTTP Request sampler.
Going forward if you're able to successfully run the request using Postman you can just record it with JMeter's HTTP(S) Test Script Recorder
Start JMeter's HTTP(S) Test Script Recorder
Import JMeter's certificate into Postman
Configure Postman to use JMeter as the proxy
Copy the file you will be uploading to the "bin" folder of your JMeter installation
Run your request in Postman
That's it, now you should have appropriate JMeter configuration for the file upload.
More information: How to Convert Your Postman API Tests to JMeter for Scaling

camunda deployment rest API error - multipart/form-data cannot be processed

I am trying to deploy my BPMN to camunda using their REST API.
URL - http://localhost:8080/engine-rest/deployment/create (Method POST)
Headers - Content-Type - multipart/form-data
The body type is form-data in postman. When I select my BPMN file and send the request I am getting this error.. why is it not working,
{
"type": "RestException",
"message": "multipart/form-data cannot be processed"
}
There is no need to set the header separately.

Open Feign Java Document Upload Corrupting

I am having an issue with my open feign client uploading documents to a backend API. I have been able to upload a document successfully using Postman, but when I use the exact same settings on my open feign client, the uploaded pdf becomes corrupted and cannot be opened.
#RequestLine("POST /url")
#Headers({
"Content-type: x-www-form-urlencoded",
})
DocumentUpload uploadDocument(#Param("string") String string, #Param("file") File file);
The url, content type, and parameters are all the same in Postman, and as far as I can tell, there are no other settings that I am missing. Has anyone had similar issues or have ideas on what to troubleshoot? Of note, I have tried different content types (eg multipart/form-data) and they fail similarly.

How do I send an Object and a file in a single HTTP request in java?

I need to send a java object and a file via HTTP in a single HTTP request. I am facing following issue:
I am using org.apache.commons.httpclient.HttpClient implementation for this and the problem is the servlet on the server side expects the multipart request as mentioned in the request and hence the Object which I send as a byte[] stream gets corrupted. If I just either file or object in a single request then this works fine.
Have you tried this tutorial: http://evgenyg.wordpress.com/2010/05/01/uploading-files-multipart-post-apache/ ?

Categories