I am trying to pass XML in a URL call and cannot seem to get the parameters right. I am being returned 415 - invalid media type. I know that if I run this URL with the same XML in my REST client (within Firefox) once I put the application/xml header it works fine. However, when I try to do the same in my Java program I get the 415 error.
Where am I going wrong? I think I am defining the content-type properly. I get the error on the line that gets the input stream.
***************** my code ***********************************
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/xml; charset=\"utf-8\"");
connection.setRequestProperty("Authorization", "Basic " + new String(new Base64().encode((strUserName + ":" + strPassword).getBytes())));
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream(), "UTF8");
output.write(strXML);
output.flush();
Document document = builder.parse(connection.getInputStream());
//pull out nodes building a node list
nodeList = document.getElementsByTagName(strElement);
connection.disconnect();
***************** my code ***********************************
***************** my XML **********************************
<?xml version="1.0" encoding="UTF-8"?>\
<rs:model-request throttlesize="5"\
xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"\
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">\
<rs:target-models>\
<rs:models-search>\
<rs:search-criteria\
xmlns="http://www.ca.com/spectrum/restful/schema/filter">\
<filtered-models>\
<equals>\
<attribute id="0x11ee8">\
<value>15</value> <!-- RTR_Cisco -->\
</attribute>\
</equals>\
</filtered-models>\
</rs:search-criteria>\
</rs:models-search>\
</rs:target-models>\
<rs:requested-attribute id="0x1006e" />\
<rs:requested-attribute id="0x10000" />\
<rs:requested-attribute id="0x10032" />\
<rs:requested-attribute id="0x12de2" />\
</rs:model-request>\
***************** my XML **********************************