1

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 **********************************

efultz
  • 1,135
  • 2
  • 11
  • 26

1 Answers1

0

I found my problem. It seems that the XML I was plugging into my string variable was missing some key spaces. Specifically I put the XML in a properties file (to simplify making changes down the road). When I did this I, inadvertently, put the continuation characters so that some spaces at end of lines were missing. So, to solve my problem I did 2 things:

1) added needed spaces in several locations 2) removed the "charset=utf-8" from the content-type property so that my setRequestProperty for Content-Type is now only setting to "application/xml"

Thank you to JP Moresmau for being my sounding board. It helped me think through solutions to, eventually, find my problem.

efultz
  • 1,135
  • 2
  • 11
  • 26