• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Why am I getting a 415 Unsupported Media Type ?

 
Ranch Hand
Posts: 670
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this end point which will take in a petId and a Multipart file but no matter how I tried in postman, I just get

this error :


'timestamp" : "asdfadsfa",
"status" : 415,
"error" : "Unsupported Media Type",
"message": "",
"path": "/pet/2/uploadImage"



Here's my code:

It is tweaked from


//https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/



as suggested from my 'mentor'


I also tried to use ResponseEntity<UploadFileResponse> like what the sample code did, without success.

Then, I tried to convert it into Base64 and changed to produces produces = MediaType.JPGE , also not successful but obviously for this uescase they are looking for a file as Json.

Another thing I noticed is that, there is no print out of





so I wonder if did get into the method at all.

The console shows the following error :



I hope some one can point out what is wrong.

BTW, the is a pet before petId/uploadImage route.



 
Sheriff
Posts: 22042
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:


How did that content type get created? Because the format is wrong; instead of multipart/form-data, boundary = ----------a bunch of digits' not supported it should be multipart/form-data; boundary = ----------a bunch of digits' not supported. In other words, between multipart/form-data and the boundary there should be a semi-colon, not a comma.

I'd also suggest not to use a boundary that start with any dashes, because multipart/form-data bodies use two dashes to indicate the start of new parts, and the end of the overall body. And I don't know if the spaces around the = are actually supported; I never add them.
 
Saloon Keeper
Posts: 22803
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be an API method for MIME-encoding response and that API would be responsible for both the Base64 encoding and wrapping the MIME identification headers around the encoded data.

The original Internet was required to be wholly text-based, since the types of machines and OS's that data could traverse varied considerably in byte size and endian-ness. So MIME was developed to allow binary data to travel safely over a text transport. In other words, MIME is very old, and very well supported.

In fact, let me quote the JavaDocs:

javax.servlet.ServletResponse wrote:
To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.



In the case of Spring, expect that there should be Spring services to make the job even easier so you don't have to do all that stuff manually, but that's what the underlying logic should rely on.
 
tangara goh
Ranch Hand
Posts: 670
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:There should be an API method for MIME-encoding response and that API would be responsible for both the Base64 encoding and wrapping the MIME identification headers around the encoded data.



In the case of Spring, expect that there should be Spring services to make the job even easier so you don't have to do all that stuff manually, but that's what the underlying logic should rely on.



Hi Tim,

Could you let me know specifically what is the specific Spring API that I can make use of that can work with java.io.Files ?

Because I tried InputStream and OutputStream but I am not able to get image inserted. So, am not sure if I am using the right API.



Furthermore, I am not able to get a json output when using Postman...all it is returning is {}...(maybe this has to be posted as another question).

I hope you can advise me on the above.

Tks.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic