Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP 415 while sending JSON object using POST

Tags:

People also ask

How do I fix http error 415?

Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

What does HTTP 415 mean?

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.

How do I fix unsupported media in Java?

Here are the three most common ways for fixing a 415 Unsupported Media Type: Make sure that you are sending the right Content-Type header value. Confirm that the server can process the value defined in the Content-Type header. Check the Accept header to see what the server can process.

How do you find unsupported media?

You can find your unsupported videos on your computer or mobile browser. To see your unsupported videos: On your Android phone or tablet, open a browser. Go to https://photos.google.com/unsupportedvideos.


import java.io.BufferedReader;    
import java.io.InputStreamReader;    
import java.net.HttpURLConnection;    
import java.net.URL;    
import java.io.DataOutputStream;        
import java.io.InputStream;

public class TestingPost {

 public static void main(String args[]) {

    URL url;
    HttpURLConnection connection = null;  
    String targetURL=".....";//here is my local server url
    String urlParameters="{\"clubhash\":\"100457d41b9-ab22-4825-9393-ac7f6e8ff961\",\"username\":\"anonymous\",\"message\":\"simply awesome\",\"timestamp\":\"2012/11/05 13:00:00\"}";

    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");

      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  

      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      System.out.println("message="+response.toString());

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      if(connection != null) {
        connection.disconnect(); 
      }
    }
  }

}

I am trying to send a JSON Object using the HTTP POST method. Above is the code but I am getting

java.io.IOException: Server returned HTTP response code: 415 for URL: ....
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at TestingPost.main(TestingPost.java:38)"

What is wrong with my code?