0

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?

3
  • 1
    Between Google and the Neo4j docs, there are plenty of examples of how to send POST requests to a Neo4j server, e.g. via a Jersey client. Unless I'm misunderstanding the question here. Commented May 2, 2014 at 14:15
  • I saw that. But I am wondering why my sample with the standard Apache http client is not working. Commented May 2, 2014 at 21:06
  • 1
    You are misunderstanding what each part of the http request is for url-parameters != headers != body, the JSON payload belongs into the body, the rest are http request headers. Commented May 4, 2014 at 21:57

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.