cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to pass a variable to the HTTP request in API call?

lingvisa
Graph Fellow
URL='http://localhost:7474/db/neo4j/tx'
headers = {'Accept': 'application/json;charset=UTF-8','Content-Type':'application/json'}

def get_nodes(label, limit_count=50)
  data = {
    "statements" : [ 
        {
          "statement" : "MATCH (n:$label) RETURN n.name LIMIT $nproduct",
          "parameters" : { "nproduct" : $limit_count } 
        } ]
  }
  r = requests.post(URL, headers = headers,json=data)
  r.json()['results'][0]['data']

In this query, I intend to query all nodes of a label, i.e. "Product". In this function, how to pass $label, and $limit_count in this function definition?

The current form doesn't work.

2 REPLIES 2

Hi there,

I don't know exactly what "doesn't work" in your implementation, but here is a modified version of your code that works for me.

WARNING It seems the API changed in Neo4j 4 compared to previous versions, the first version below is for Neo4j 4:

import requests

headers = {
    'Accept': 'application/json;charset=UTF-8',
    'Content-Type':'application/json',
    'Authorization': 'Basic XXX'
}

URL_V4 = "http://localhost:7474/db/neo4j/tx/commit"
def get_nodes_neo4j_4(label, limit_count=50):
    data = {
        "statements" : [ 
            {
            "statement" : "MATCH (n) WHERE $label IN labels(n) RETURN n.name LIMIT $nproduct",
            "parameters" : {
                "label": label,
                "nproduct" : limit_count
                },
            }
        ]
    }
    r = requests.post(URL_V4, headers=headers, json=data)
    return r.json()

res = get_nodes_neo4j_4("Product", 3)
print(res["results"][0]["data"])

Related SO question: https://stackoverflow.com/questions/24274364/in-neo4j-how-to-set-the-label-as-a-parameter-in-a-cyphe...

NB: equivalent for Neo4j 3:

URL_V3 = 'http://localhost:7474/db/data/transaction/commit'
def get_nodes_neo4j_3(label, limit_count=50):
    data = {
        "statements" : [ 
            {
            "statement" : "MATCH (n) WHERE {label} IN labels(n) RETURN n.name LIMIT {nproduct}",
            "parameters" : {
                "label": label,
                "nproduct" : limit_count
                },
            }
        ]
    }
    r = requests.post(URL_V3, headers=headers, json=data)
    return r.json()

lingvisa
Graph Fellow

Hi, Estelle:

It worked using the V3 URL version a few days ago. However, I had to re-install my db and it works all fine when i look at it in the browser or the desktop app. However, in the API call, it gives this error:

b'{\n "errors" : [ {\n "code" : "Neo.ClientError.Security.Unauthorized",\n "message" : "No authentication header supplied."\n } ]\n}'

Should I supply the user name and password somehow in the header:
username: neo4j
password: 123456

However, in the config file, I already disabled this line:
#dbms.security.auth_enabled=true

Why does it still report this error? It that's the case, how to provide username/password to the header?

URL_V3 = 'http://localhost:7474/db/data/transaction/commit'
def get_nodes_neo4j_3(label, limit_count=50):
    data = {
        "statements" : [ 
            {
            "statement" : "MATCH (n) WHERE {label} IN labels(n) RETURN n.name LIMIT {nproduct}",
            "parameters" : {
                "label": label,
                "nproduct" : limit_count
                },
            }
        ]
    }
    r = requests.post(URL_V3, headers=headers, json=data)
    return r.json()