SoFunction
Updated on 2025-03-03

About Unsupported Media Type Solution

I encountered a problem today, when one of my own interfaces was called back by a third-party business, a 415 error occurred, that is, Unsupported Media Type. Afterwards, I checked some information and summarized it.

What is the error of 415?

For the currently requested method and the requested resource, the entity submitted in the request is not the supported format in the server, so the request is denied.

In other words, the parameter format of the server sent to it by the third party is different from the parameter format required by the server.

For example, the third party passes the application/x-www-form-urlencoded;charset=UTF-8' (the default format of the front-end form will be used and connected to the parameters of the key-value pairs.

If there are spaces, convert the spaces to + plus signs; if there are special signs, convert the special signs to ASCII HEX values), and the server needs the json format, which will cause a 415 error.

Two solutions

There are two main types that correspond to the server and the client respectively

  • Server: Adjust the way the interface receives parameters. For example, if the parameters in json string format are received, use @RequestBody annotation to receive the whole; if the parameters connected with the & are received, use @RequestParam annotation to receive them one by one.
  • Client: Adjust the format of upload parameters and pass them according to the parameters required by the interface.

By the way, record common media format types

Common media format types

  • text/html: HTML format
  • text/xml: XML format
  • text/plain: plain text format
  • image/gif: gif image format
  • image/jpeg: jpg image format
  • image/png: png image format

Media format type starting with application

  • application/x-www-form-urlencoded: The default format in the form form, the form form data is encoded in key/value format and sent to the server.
  • application/json: JSON data format
  • application/xml: XML data format
  • application/xhtml+xml: XHTML format
  • application/atom+xml: Atom XML aggregation format
  • application/pdf: pdf format
  • application/msword: Word document format
  • application/octet-stream: binary stream data (such as common file downloads)

Of course, there is also a file upload format used in the form form: multipart/form-data will not be written into the above two categories.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.