Open Feign Java Document Upload Corrupting - java

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.

Related

Jmeter File upload Scenario - Issue PUT with body

I have a jmeter scenario where I need to upload a txt file of different size . To upload the file in jmeter , we enable multipart and request is of PUT type.
I am also getting put body , which is creating issue in my log files in the backend . Posting a sample request body . Is there any way where we can just send data without any data showing up under put data .
According to your screenshot you're trying to upload a file to http://www.csm-testcenter.org/test page, the page assumes HTTP POST method, not PUT.
If you're uncertain regarding proper JMeter configuration when it comes to uploading files be informed that you can just record the file upload event using your favourite browser (or another application) and JMeter's HTTP(S) Test Script Recorder. All you need to do is to copy the file to "bin" folder of your JMeter installation, this way JMeter will be able to properly capture the request and generate the relevant HTTP Request sampler and HTTP Header Manager.
More information: Recording File Uploads with JMeter

How to correctly upload an attachment with Apache HTTPClient to RTC (IBM Jazz)?

I am currently facing the following issue with a Java program.
I am trying to upload a file I downloaded from an external server (file type can be different for every execution) to a IBM RTC (or Jazz) server through REST call.
The REST endpoint I am sending the first request (i.e.: the file upload to the central repository) is as the following:
https://jazz.host.com:1234/ccm/service/com.ibm.team.workitem.service.internal.rest.IAttachmentRestService/?projectId=<TEAM_AREA_ID>&multiple=true&category=<CATEGORY_ID>
And this step works, uploading the file to the central repo. What it is not working is the fact that when the attachment name contains some kind of characters (i.e.: £, ù, °, ...) those are changed into the character ?.
I am using Apache HttpClient with a CloseableHttpClient, preparing the HttpEntity to be sent as follows:
java.io.File currentAttachmentFile = utils.getFileFromPath(attachmentPath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", currentAttachmentFile, ContentType.MULTIPART_FORM_DATA, currentAttachmentFile.getName());
return builder.build();
But even sepcifying the withCharset(StandardCharsets.UTF8) to ContentType.MULTIPART_FORM_DATA does not do the trick, the file name stile contains the ? character (while I expect it to be something like: 'file_name 1!==£$%&^+ù°##'.png').
I took a look around for other suggestions but neither those found here and here are working for the file name. Moreover, specifying the Content-Type to the POST call I am sending causes a failure with the message 'Invalid POST request message' returned by RTC, so this can't be an option.
Through PostMan, the upload with the same file to the same endpoint works so I suppose there is something I am missing when creating the entity to send to RTC.
Does anyone know how to solve this?
Best regards,
Alberto

Spring Rest - Post multiple files

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.

How can I fix org.jvnet.mimepull.MIMEParsingException?

While uploading a image/doc/xlsx file from my AngularJS client to my server-side java using JAX-RS(Jersey) i am getting the following exception,
org.jvnet.mimepull.MIMEParsingException: Reached EOF, but there is no closing MIME boundary.
What is this? Why I am getting this exception? How can I get rid of this?
Note: It works for the files with extension .txt, .html, .yml, .java, .properties
But not working for the for the file with extension .doc, .xlsx, .png, .PNG, .jpeg.. etc.
My Server side code:
#POST
#Path("/{name}")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadedFiles(#Nonnull #PathParam("name") final String name,
#FormDataParam("file") final InputStream inputStream,
#FormDataParam("file") final FormDataContentDisposition content) {
}
I encountered the same issue. Based on my research, the problem has no relation with the file type. It has a little relation with the size of the uploaded file.
I'm not sure if the root cause is when the uploading file is very big, before the file is uploaded to the server completely, the client disconnects to the server (such as timeout). And I also verified the guess. My test steps is,
1. In client, upload a very big file.
2. Before the get the response from server, which means is uploading file;
close the test client
3. check the server side, you will see the issue.
So To fix it, my solution is add timeout time in client side.
OK, I'm only guessing, but I think I can see a pattern here.
The file types that are working are text based
The file types that are not working are binary
This suggests to me that maybe the problem is that there is some kind of issue with the way that non-text data is being handled by the upload process. Maybe it is being transcoded when it shouldn't be.
Anyway, I suggest that you use some tool like Wireshark to capture the TCP/IP traffic in an upload to see if the upload request body has valid MIME encapsulation.

Specify actual content-type of a uploaded file

I'm working on a blob upload service and I have this code that persist uploaded meta-data in the Datastore and the actual bytes into the Blobstore.
I am having issues with the Mime-Type provided by the REST client (I am using the Rest Console Chrome plugin)
This is the mime-type that is passed from the client to the server:
Mime type: multipart/form-data; boundary=----WebKitFormBoundarytxCVEFIRjPB7YIq7
However the server needs to get the "actual mime-type", that is, for example image/jpeg, image/jpeg, text/plain, etc.
Using the App Engine API I need to explicitly provide the mime-type like this:
AppEngineFile file = fileService.createNewBlobFile(entity.getMimeType());
How do I specify this? Do I need to put another header? The files that will be uploaded will vary and that the file can be up to 50MB in size (If I remember that correctly).
"Multipart" means that the data sent can have multiple parts, each with it's own mime type. Read about handling multipart form uploads on GAE.
You can use FileItemStream.getContentType() to get mime type of each part.

Categories