Http 415 Error on unsupported media type using JSON

I call the REST service with a JSON request and it returns an Http 415 error "Unsupported media type."

The content type of the request matters ("Content Type", "app / json; charset = utf8").

It works fine if I do not include the Json object in the request. I am using google-gson-2.2.4 library for json.

I tried using several different libraries, but that didn't matter.

Can someone help me solve this?

Here is my code:

public static void main(String[] args) throws Exception { JsonObject requestJson = new JsonObject(); String url = "xxx"; //method call for generating json requestJson = generateJSON(); URL myurl = new URL(url); HttpURLConnection con = (HttpURLConnection)myurl.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json; charset=utf8"); con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Method", "POST"); OutputStream os = con.getOutputStream(); os.write(requestJson.toString().getBytes("UTF-8")); os.close(); StringBuilder sb = new StringBuilder(); int HttpResult =con.getResponseCode(); if(HttpResult ==HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); System.out.println(""+sb.toString()); }else{ System.out.println(con.getResponseCode()); System.out.println(con.getResponseMessage()); } } public static JsonObject generateJSON () throws MalformedURLException { String s = "http://www.abc.com"; s.replaceAll("/", "\\/"); JsonObject reqparam=new JsonObject(); reqparam.addProperty("type", "arl"); reqparam.addProperty("action", "remove"); reqparam.addProperty("domain", "staging"); reqparam.addProperty("objects", s); return reqparam; } } 

RequestJson.toString value:

{"type": "arl", "action": "remove", "domain": "staging", "objects": " http://www.abc.com "}

+95
Mar 21 '14 at 18:11
source share
12 answers

Not sure about the reason, but removing the charset=utf8 lines from con.setRequestProperty("Content-Type", "application/json; charset=utf8") resolved the problem.

+73
Mar 25 '14 at 19:02
source

Add Content-Type: application/json and accept: application/json

+39
Sep 15 '16 at 6:45
source

This is because charset=utf8 should be without space after application/json . This will work fine. Use it as application/json;charset=utf-8

+9
May 17 '16 at 15:34
source

If you are making a jquery ajax request don't forget to add

 contentType:'application/json' 
+7
Sep 13 '16 at 16:49
source

Add an HTTP header manager and add the names and values ​​of the API headers to it. e.g. Content-type, Accept, etc. This will solve your problem.

+2
Nov 29 '17 at 5:27
source

Several times Charset Metada aborts json when sending a request. It is better not to use charset = utf8 in the request type.

+1
May 08 '17 at 17:01
source

If you are using AJAX jQuery Request, this must be applied. If not, it will cause error 415 .

 dataType: "json", contentType:'application/json' 
+1
May 16 '18 at 12:26
source

I sent a delete request and it didn’t work with 415. I saw what type of content my server uses to get into the api. In my case, it was "application / json" instead of "application / json; charset = utf8".

So ask your api developer. And in the meantime, try sending the request only with content-type = "application / json".

0
Apr 14 '16 at 9:03
source

I had the same problem. My problem was a complex object to serialize. One attribute of my object was Map<Object1, List<Object2>> . I changed this attribute as List<Object3> , where Object3 contains Object1 and Object2 , and everything works fine.

0
Jan 12 '17 at 22:12
source

I know that it is too late to help the OP with its problem, but to all of us who are just facing this problem, I solved this problem by removing the constructor with the parameters of my class, which is designed to store json data.

0
Aug 30 '17 at 7:17
source

Add MappingJackson2HttpMessageConverter manually in the configuration solved the problem for me:

 @EnableWebMvc @Configuration @ComponentScan public class RestConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) { messageConverters.add(new MappingJackson2HttpMessageConverter()); super.configureMessageConverters(messageConverters); } } 
0
May 31 '18 at 11:44
source

The reason may not be adding “annotations” to your dispatcher servlet XML file. and may also be due to the fact that the application / json is not added in the headers

0
Jul 20 '18 at 5:06
source



All Articles