Note that there are some explanatory texts on larger screens.

plurals
  1. POReST: POST: java.io.IOException: Unsupported Media Type
    text
    copied!<p>I'm trying to make a POST call to a local ReST service that sends back a simple XML response.</p> <p>I'm getting back this error:</p> <pre><code>java.io.IOException: Unsupported Media Type at com.eric.RawTestPOST.httpPost(RawTestPOST.java:42) at com.eric.RawTestPOST.main(RawTestPOST.java:66) </code></pre> <p>I'm following this example:<a href="http://rest.elkstein.org/2008/02/using-rest-in-java.html" rel="nofollow">Link</a></p> <p>Here is my code:</p> <pre><code>public class RawTestPOST { public static String httpPost(String urlStr, String method, String parameter, String parameterValue) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Create the form content OutputStream out = conn.getOutputStream(); Writer writer = new OutputStreamWriter(out, "UTF-8"); /* for (int i = 0; i &lt; string.length; i++) { */ writer.write(method); writer.write("?"); writer.write(parameter); writer.write("="); writer.write(URLEncoder.encode(parameterValue, "UTF-8")); writer.write("&amp;"); /* } */ writer.close(); out.close(); if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } // Buffer the result into a string BufferedReader rd = new BufferedReader(new InputStreamReader(conn .getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); return sb.toString(); } public static void main(String[] args) { String url = "http://localhost:9082/ServicesWSRest/"; String method = "getResponse"; String parameter = "empID"; String parameterValue = "954"; try { System.out.println(RawTestPOST.httpPost(url, method, parameter, parameterValue)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>The parameters are not imprtant. The XML response just returns the parameters sent in.</p> <p>I can get it to work with a GET request.</p> <p>Let me know if y'all need anymore information.</p> <p>Thanks, E</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload