0

I'm not sure what exactly I can do to see what my issue is with my POST request. I had made a quick prototype in PHP using the CURL libraries to construct my request. When I try to do the same function in a JSP page I am getting a HTTP error 415. I'm unsure how to fix it. I have gotten the correct in both the PHP page and on a web debugger (fiddler) without error. I was also able to do a successful get request on this JSP page with another part of the REST api I'm using. What can I do to find the issue?

Here is the JSP code

String xmlRequest = "<viewParameters><vslText>[field1] = '01510486'</vslText></viewParameters>";
try
{
    URL url = new URL("*****");
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    //Connection Properties
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setFollowRedirects(true);

    //Header Parameters
    conn.addRequestProperty("X-IntegrationServer-Username", "******");
    conn.addRequestProperty("X-IntegrationServer-Password", "*******");
    conn.setRequestProperty("ContentType", "application/xml");
    conn.setRequestProperty("Host", "*********");
    conn.setRequestProperty("Content", "viewParameters");
    conn.setRequestProperty("Accept", "application/xml");

    //Post Parameter
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
    writer.write(xmlRequest);

    conn.connect();

    //CHECK HTML RESPONSE 
    out.print("<br>Response code = " + conn.getResponseCode());
    out.print("<br>Response message = " + conn.getResponseMessage() + "<br><br>");

    //Output Here
    InputStream input = conn.getInputStream();

    //Takes XML and puts it into one string variable
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String inline = "";
    while ((inline = inputReader.readLine()) != null) {
      sb.append(inline);
    }


    inputReader.close(); 

    out.print(sb.toString());

    } catch (Exception e) {
e.printStackTrace();
}

Here is the working PHP code

<?php 

$curl = curl_init();

//xml to send to server
$xmlRequest = "<viewParameters><vslText>[field1] = '********'</vslText></viewParameters>";

//took out 'Content-Length: ' . strlen($xmlRequest)
$requestParam = array('X-IntegrationServer-Username: ********', 'X-IntegrationServer-Password: ************', 'Host: *************', 'Content-Type: application/xml', 'Content: viewParameters');

curl_setopt($curl, CURLOPT_HTTPHEADER, $requestParam);

curl_setopt($curl , CURLOPT_URL, '***************');

//stops SSL errors but also turns off security for it check this link   http://stackoverflow.com/questions/21187946/60-ssl-certificate-self-signed-certificate-in-certificate-chainbool
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlRequest); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);

//if no response comes through tries to give error message
if(!$response){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

curl_close($curl);

$xml = simplexml_load_string($response);
Crash667
  • 343
  • 2
  • 16
  • 415 error = _The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method._ So I guess you missed something in the conversion – RiggsFolly Jul 28 '14 at 16:21
  • where is `xmlRequest` defined on your jsp code ? – Jorge Campos Jul 28 '14 at 16:21
  • Have you viewed the response message? There should be a message that comes along with the error 415. 415 is an Unsupported media type. Are you uploading any files to the server? – War10ck Jul 28 '14 at 16:22
  • @RiggsFolly I don't know where I really need to convert anything. Maybe it's something that I didn't need to do with the CURL library but I didn't for php. – Crash667 Jul 28 '14 at 16:26
  • @Jorge Campos sorry its just before the try block, I'll edit to show. – Crash667 Jul 28 '14 at 16:27
  • @War10ck It just says the code is 415 and the message is Unsupported media type. I am only trying to upload a xml string that was specified by the third party REST API I am using. – Crash667 Jul 28 '14 at 16:28

1 Answers1

0

remember the dash.....

conn.setRequestProperty("ContentType", "application/xml");

should have been

conn.setRequestProperty("Content-Type", "application/xml");
Crash667
  • 343
  • 2
  • 16