0

I'm looking for a code example creating a REST POST request with JQuery on Neo4j 2.2.x Transactional Cypher HTTP endpoint with new REST API Authentication and Authorization parameters.

Before Neo4j 2.2 version I used the Legacy Cypher HTTP endpoint (which is deprecated) to execute Cypher queries with the following code:

$.post("http://localhost:7474/db/data/transaction/commit",
{
    "query": query,
    "params": {}
}, "json")...

But I would like to move to 2.2 and use the transactional endpoint with user authentication parameters.

I can't find the right headers and parameters combination to use to create such a request. That's why I'm looking for a code example.

Best would be an example using a cross domain request but an example on same domain would be helpful too.

3 Answers 3

2

For authentication you need to supply an additional HTTP header to your request:

Authorization: Basic realm="Neo4j" <base64>

where <base64> is the base64 encoded string of username:password.

Not being a jquery ninja, but I guess the most simple way is to set the authorization header using ajax defaults:

$.ajaxSetup({
   headers: { "Authorization": 'Basic realm="Neo4j' <base64>'}
});

When this default has been applied your $.post above should work.

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

1 Comment

I already tried with this header but I get the following error: Failed to load resource: the server responded with a status of 401 (Unauthorized) tempsajax.html:1 XMLHttpRequest cannot load localhost:7474/db/data/transaction/commit. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:63342' is therefore not allowed access. The response had HTTP status code 401. I use both server in localhost but I think this is related to cross domain issues.
1

The issue has been fixed in 2.2.0-RC01 and I can use transactional Cypher HTTP endpoint with authentication using the following example code:

$.ajaxSetup({
    headers: {
        // Add authorization header in all ajax requests
        // bmVvNGo6cGFzc3dvcmQ= is "password" base64 encoded
        "Authorization": "Basic bmVvNGo6cGFzc3dvcmQ=" 
    }
});

$.ajax({
    type: "POST",
    url: "http://localhost:7474/db/data/transaction/commit ",
    dataType: "json",
    contentType: "application/json;charset=UTF-8",
    data: JSON.stringify({"statements": [{"statement": "MATCH (n) RETURN n LIMIT 10"}]}),
    success: function (data, textStatus, jqXHR) {
        // use result data...
    },
    error: function (jqXHR, textStatus, errorThrown) {
        // handle errors
    }
});

Comments

0

Authorization means that the browser will send a preflight OPTIONS request which does not embed authorization headers.

This is most known as CORS.

Currently the Neo4j server do not reply to the OPTIONS request with the appropriate Access-Control-Allow-Headers. This feature has been implemented in the source code and will be shipped with the GA release which I hope will come out this week.

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.