简体   繁体   中英

415 Unsupported MediaType for Content-Type = Mulitpart/form-data

I have a REST API which ONLY @Produces(MediaType.APPLICATION_JSON) and does not @Consume any MediaType.

Even still when I am consuming this REST API by creating an HTTP Request where MediaType is being set to multipart/form-data , I am still recieving:

HTTP/1.1 415 Unsupported Media Type

in raw HTTP Response .

However, when I am setting Content-Type to application/json , which this REST API @Produces , I am getting a proper HTTP Response .

Below is the REST API Annotations:

@POST
@Path("/somePath")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject addDocument(AddDocBean addDocBean) {
...

"Now why do I want to send Content-Type as Multipart/form-data ?"

That is because I am using this service to upload a file to the server and the file is to be sent in a BASE64 format. Now the size of file is about 20Mb which I have been suggested is better sent as multipart/form-data

But HTTP server is unable to process this request returning ERROR 415 Unsupported MediaType in response.

REST client being used : SoapUI 5.4.0

What could possibly be wrong?

  • @Produces on Server Side corresponds to Accept: Header on the Client Side.
  • @Consume on Server Side corresponds to Media-Type: Header on the Client Side.

The API snippet you showed doesn't specify any explicit @Consume annotation and hence defaults to plain/text. And hence when you send the Header of application/multipart with your request you get Unsupported Media Type

You need to add an explicit @Consume("MediaType.MULTIPART_FORM_DATA") // something similiar to this over your API to make your addDocument() method to support the form data that you are sending.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM