Attachments/REST WS: Error 415 (Unsupported Media Type)

@mksd and others

I am trying to use attachment module 2.1.0 to upload patient images etc. In CentOS linux, While i upload the image, it gives me the error Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)

Nothing on the server side though and application does not go forward

Hi @kshebbar,

About the HTTP error 415:

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

The format problem might be due to the request’s indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.

At the first sight it looks like this error is not related to Attachments itself but to an underlying error 415 linked to the file that you attempted to upload. Can you reproduce this with the same file on the Ref App QA server? Here: https://qa-refapp.openmrs.org

Hi @mksd Thanks for the quick response. Tried at QA server. I uploaded png file and it gave an error for patient. The error is different with unknown object (404 - Not Found) where in our centOS it was 415 error.

Can you paste here the URL that gives an error 404 on QA Ref App?

While in the screen I get Object could not be found, the browser console gives 404 in the background. The url is https://qa-refapp.openmrs.org/openmrs/attachments/attachments.page?patient=8180149e-5f36-4ad6-b5a9-c7b12a6e7ec9&patientId=227&returnUrl=%2Fopenmrs%2Fcoreapps%2Fclinicianfacing%2Fpatient.page%3FpatientId%3D8180149e-5f36-4ad6-b5a9-c7b12a6e7ec9%26&

I drag/drop a file and click on upload

I would need the exact URL that gives a 404 error, the one you provided just takes me to some patient’s attachments page.

As i mentioned it is not classic 404 file not found error; if you drag an image and click on upload, and then observe inspect/console, it shows 404. Bottomline, attachment is not working

Ok on the QA server there is an issue because it runs Attachments release 2.0.0 and its REST API is strictly limited to Core 2.0.x while the QA server runs 2.1.4 (see here).

This was the 404 error URL btw.

@dkayiwa this means that the version of Attachments will have to be bumped from 2.0.0 tot 2.1.0-SNAPSHOT on the distro, is that still ok?


@kshebbar ok so unfortunately your original issue can’t be reproduced on the QA server. This will make it more difficult to troubleshoot. Any more details about what you experienced on your CentOS server? Any logs, is this happening with a specific file, … etc?

The original HTTP error refers to either an unsupported format or MIME type, so it suggests that a specific file might be causing the error.

Thanks, Somehow in Windows it is w0rking; snippets of error as below

-------------------------------------
<entry>
<string>message</string>
<string>
The Resource Does not Support the Requested Operation [null]
</string>
</entry>
<entry>
<string>code</string>
<string>
org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource:230
</string>
</entry>
<entry>
<string>detail</string>
<string>
org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException at org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.doGetAll(DelegatingCrudResource.java:230) at org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.getAll(DelegatingCrudResource.java:217) at org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.get(MainResourceController.java:201) at sun.reflect.GeneratedMethodAccessor1117.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177) at
----------------------------------------------------------------------------------------
dropzone.js?cache=1552222135673:1386 POST /openmrs/ws/rest/v1/attachment 415 (Unsupported Media Type)
------------------------------------------------------------------------------------------
<body><h1>HTTP Status 415 – Unsupported Media Type</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Content-Type multipart/form-data; boundary=----WebKitFormBoundaryeiRw9zkYXlC0fDGF not supported</p><p><b>Description</b> The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.</p><hr class="line" /><h3>Apache Tomcat/7.0.93</h3></body>
----------------------------------------------------------------------------------------------------------------

Somehow in Windows it is w0rking;

You mean when you run OpenMRS on Windows instead of CentOS, or what did you do exactly?

yes, attachment feature works correct in Windows OS hosted server, but not in CentOS hosted server

And you get this error 415 for every single file on CentOS, even those with non-complex MIME types (such as a simple text file for example)?

yes, tried with simple txt file and it gave the same error, Earlier I was uploading pdf/png files

Can we have access to this CentOS server to try out?

Unfortunately, it is not available for outside network as of now

@mksd do you want to first release it?

Yes ok I’ll do that and PR for an upgrade in the current distro master.

Googling around suggests that somehow the content type might not passed properly in the HTTP request. Since you are in a position to put side to side a working case (Windows) and problem case (CentOS), you are therefore in a position to compare exactly what might differ in the two scenarios and pinpoint the issue.

Of course while doing this you should use the same client (browser) and the exact same files.

thats correct, it says content-type to be set to application/json; in the log above it shows multipart/form-data. Not sure whether thats the solution

For everyone’s reference this is where the multipart file is obtained as part of the request in REST WS:

@RequestMapping(.., headers = "Content-Type=multipart/form-data")
@ResponseBody
public Object upload(.., @RequestParam("file") MultipartFile file, ..)

If you Google around and follow threads on Stack Overflow about error 415 and Spring, a number of posts talk about how this should be done. However as far as I can tell the above seems ok.

… except maybe that the Accept header might have to be added (see here), and I’m not sure what the syntax would then be. Maybe this?

@RequestMapping(.., headers = "Accept=multipart/form-data", produces = "multipart/form-data")

@kshebbar are you in a position to try this by forking REST WS?