Http 415 Unsupported Media type error with JSON
Not sure about the reason but Removing lines charset=utf8
from con.setRequestProperty("Content-Type", "application/json; charset=utf8")
resolved the issue.
415 (Unsupported Media Type) with REST Post request
Couple of things I noticed.
- You're trying to do a
POST
request with a JSON body. On the client, your request looks fine.
As I understand the POST body is
{ clientid: 'some-client-id' }
- The interesting thing is in the web API you receive it as
public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid)
This is possibly the culprit. Your API is expecting a string as a POST body where it is a json object. Have you tried changing the type to dynamic
or JObject
?
So, essentially,
public async Task<IHttpActionResult> SetTenantActive([FromBody]dynamic clientRequest)
OR
public async Task<IHttpActionResult> SetTenantActive([FromBody]JObject clientRequest)
Alternately,
If you want to continue using your API as is, then you can just change the request you’re making from the client to ’some-client-id’
instead of { clientid: 'some-client-id' }
How to fix 415 Unsupported Media Type for Rest API between Angular 7 and Java
You are sending 'application/x-www-form-urlencoded'
as content-type
. You will need to specify that in the REST API. You should include consumes
parameter in your @RequestMapping
. Something like this
@RequestMapping(value = "/admin/login", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, headers = "Accept=application/json")
You will also need to remove the @RequestBody
annotation on the method parameter
// removed @RequestBody annotation
public Map login(AdminLogin parameters) {
// .... body
}
You will need to use RequestParam
annotation and modify your method like so
// using map to get all the keys & their values.
public Map login(@RequestParam Map<String, String> map){
}
// get all the form parameter in separate method parameter like so
// (key name are dummies here, use appropriate names when using)
public Map login(@RequestParam("key1") String key1, @RequestParam("key2") String key2){
}
I also think headers
with Accept
parameter section is unnecessary here.
Image showing the api called using Postman client (ignore the url here... just testing)
Image showing the value got in the map. Notice the data sent in the API call and data received in the map object.
HTTP Error 415 Unsupported Media Type on REST service operation with an XML parameter (Jersey + Jetty )
There's a missconfiguration of the maven-shade plugin. Replacing it by
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
solves the problem
415 Unsupported Media Type in PostMan but not in curl
I can see two main problems:
- In body request you are sending an invalid Json, because you add -d before and simple quote at the end (eliminate both).
- You don't set header/content type right. To achieve it click in Text (on the right binary button) and set in JSON (aplication/json), with that postman add header for request.
Example:
PD: I tried make your request, but I never receive an 200 status code, most requests were timeout (I don't know if you run that server locally or have a special proxy configuration).
POST results in a HTTP 415 Unsupported Media Type response
POST results in a HTTP 415 Unsupported Media Type response
When I tested with your code, I did reproduce the problem.
Through debug, it is found that in the fetch
method, the contentType
needs to be added to the application/json
format to pass the json data, but there is a problem with your writing.
To sove it, just to change the content of headers in fecth as follow:
headers: { 'Content-Type': "application/json" }
Spring boot: Getting 415 unsupported Media type
Solved! I accidentally had two setters defined with different data types.
HTTP Status 415 – Unsupported Media Type when doing POST request on Spring MVC
Tried everything but couldn't it to work. Maybe I was making a silly mistake somewhere or there was something seriously wrong with my configuration. Anyway, I tried to make it work with Spring boot and it worked fine. For anyone who is interested, Here is the github link - https://github.com/Sanil2108/spring_hibernate/tree/master/spring_boot1
Also, thanks to everyone who tried to help!
Related Topics
Image Upload With Preview and Delete Option - Javascript/Jquery
Show 2 Items Per Row[React Native]
Javascript Onclick Increment Number
How to Convert Seconds to Minutes and Hours in JavaScript
How to Set an Iframe Src Attribute from a Variable in Angularjs
Javascript/Dom: How to Remove All Event Listeners of a Dom Object
Modify Classname When Element Is Clicked in React
Basic Open Source JavaScript Image Editor
Getting Selected Values from Dropdown Inside Table
Vuejs Accessing a Method from Another Method
Regular Expression Which Matches a Pattern, or Is an Empty String
Can We Have Code After Location.Reload(True)
Call Method That Inside Another Component - React Js
Remove Parameter from Url Via Regex
Vuetify: How to Preselect Active Tab
How to Handle Multiple Submit Buttons in a Form Using Angular Js