Ganesh
Ganesh

Reputation: 131

Server returned HTTP response code: 400 for URL : HttpURLConnection Error

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.

Upvotes: 0

Views: 9521

Answers (1)

hgoebl
hgoebl

Reputation: 13007

It's not your fault. The documentation of the web-service is missing an important detail:

If posting JSON, you can do it this way. When posting XML, you have to provide the XML like if posted in an HTML form: application/x-www-form-urlencoded.

In the source code of the service you can see:

def _convert(method):
    a = request.accept_mimetypes.best_match(["application/json", "text/html"])
    if a == "application/json":
        xml = request.get_data().decode("utf-8")
    else:
        xml = request.form["xml"]   # <-- xml is a form field!!
    # ...    

I've tried it with DavidWebb which is only a tiny wrapper around HttpURLConnection and it worked:

public void testNigelsmall() throws Exception {

    String payload = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
            "<group id=\"abba\">\n" +
            "  <member id=\"agnetha\">\n" +
            "    <name>Agnetha Fältskog</name>\n" +
            "    <birth date=\"1950-04-05\" />\n" +
            "  </member>\n" +
            "  <member id=\"frida\">\n" +
            "    <name>Anni-Frid Lyngstad</name>\n" +
            "    <birth date=\"1945-11-15\" />\n" +
            "  </member>\n" +
            "  <song id=\"waterloo\" release_date=\"1974-03-04\">\n" +
            "    <name>Waterloo</name>\n" +
            "    <length min=\"2\" sec=\"42\" />\n" +
            "  </song>\n" +
            "</group>";

    Webb webb = Webb.create();
    Response<String> response = webb
            .post("http://api.nigelsmall.com/xml-cypher")
            .param("xml", payload)
            .asString();

    assertEquals(200, response.getStatusCode());
    assertNotNull(response.getBody());
    assertTrue(response.getBody().startsWith("CREATE"));
    assertTrue(response.getBody().contains("(abba)-[:member]->(frida)"));
}

The output:

CREATE
(abba),
(agnetha {name:"Agnetha F\u00e4ltskog",`birth date`:"1950-04-05"}),
(frida {name:"Anni-Frid Lyngstad",`birth date`:"1945-11-15"}),
(waterloo {`length sec`:42,`length min`:2,name:"Waterloo"}),
(abba)-[:member]->(agnetha),
(abba)-[:member]->(frida),
(abba)-[:song {release_date:"1974-03-04"}]->(waterloo)

Upvotes: 1

Related Questions