Question

I have just started with neo4J and wanted to try the transactional cypher endpoint. I have my neo4J server running on localhost:7474/ and have inserted the movie data.

As stated in the documentation, I have to do a post request and include some parameters. Unfortunately I don't know how I have to include my query in the POST request. As far as I have understood it, I have to pass a JSON String.

private static String sendPost() throws Exception {

        String url = "http://localhost:7474/db/data/transaction";
        String statement ="[ { \"statement\" : \"MATCH (n:Person) RETURN n.name, n.born\"} ]"; 



        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("Accept", "application/json; charset=UTF-8"));
        urlParameters.add(new BasicNameValuePair("Content-Type", "application/json"));
        urlParameters.add(new BasicNameValuePair("statements", statement));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);

        StringBuilder builder = new StringBuilder();

        builder.append("\nSending 'POST' request to URL : " + url+"<br>");
        builder.append("Post parameters : " + post.getEntity()+"<br>");
        builder.append("Response Code : " + response.getStatusLine().getStatusCode()+"<br>");

        BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent()));



        StringBuffer result = new StringBuffer();
        result.append("<p>");
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line+"\n");
        }

        result.append("</p>");

        return builder.toString();

    }

When I execute the code, I get the following output:

Sending 'POST' request to URL : http://localhost:7474/db/data/transaction
Post parameters : org.apache.http.client.entity.UrlEncodedFormEntity@76adb5f6
Response Code : 415

Can anyone help me on how I have to include my query in the POST request?

Was it helpful?

Solution

http://docs.neo4j.org/chunked/stable/rest-api-transactional.html

Looking at that, you can see the body of your POST request isn't what the server is expecting, i.e. you should be sending an entire JSON document, and not a k/v pair w/ "statements" as a key and your JSON Cypher query as the value. Remember you're sending JSON here, and not a URLEncoded body.

Also, it looks like you're setting the "Accept" and "Content-Type" k/v pairs as part of the POST body when they should, in fact, be part of the headers.

Also also, consider using the Cypher endpoint: http://docs.neo4j.org/chunked/stable/rest-api-cypher.html

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top