I am trying to replicate the following unix cURL command in java:
curl -X POST http://api.nigelsmall.com/xml-cypher -d @test/files/abba.xml
edit:
To be more specific I am trying to replicate the following cURL command in java:
curl -X POST http://api.nigelsmall.com/xml-cypher -d "
<?xml version='1.0' encoding='UTF-8' ?><group><member></member></group>
"
So far I have the following code:
String urlString = "http://api.nigelsmall.com/xml-cypher";
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/xml");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String data = "<?xml version='1.0' encoding='UTF-8' ?><group><member></member></group>";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
out.close();
rd.close();
}
catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
But java.io.IOException: Server returned HTTP response code: 400 for URL :http://api.nigelsmall.com/xml-cypher occurs. Can anyone find what the problem is?
The only thing I can think of is that may be I need to put my XML in a file and send it as a request. But I do not know how to do this. Could anyone help me with this? Thanks in advance.