Py2neo erroring for basic requests with neo4j

I have spent the last few months running neo4j on my local machine. I have been using py2neo to orchestrate the db with python. I have just switched to running the database on an aws ec2 instance and I am recieving errors whenever I try to send requests to the database. I am using the following simple code to test the database, this code works perfectly on my local machine.

from py2neo import *

neo4j_url = 'http://localhost:7474/'
user = 'neo4j'
pwd = 'neo4j'

graph = Graph(neo4j_url, auth=(user,pwd))
print(graph)
graph.run("Create (n:Person{name: 'Andy person'})")

This code errors with

Graph('http://localhost:7474')
Traceback (most recent call last):
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 443, in from_json
    content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
  File "/usr/lib64/python3.8/json/__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "/usr/lib64/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test_connection.py", line 9, in <module>
    graph.run("Create (n:Person{name: 'Andy person'})")
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/database.py", line 405, in run
    return self.auto().run(cypher, parameters, **kwparameters)
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/database.py", line 989, in run
    result = self._connector.auto_run(cypher, parameters,
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/__init__.py", line 1340, in auto_run
    return cx.auto_run(cypher, parameters, graph_name=graph_name, readonly=readonly)
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 177, in auto_run
    rs = HTTPResponse.from_json(r.status, r.data.decode("utf-8"))
  File "/home/ec2-user/KG_construction/kg-construction/KG_env/lib64/python3.8/site-packages/py2neo/client/http.py", line 445, in from_json
    raise_from(ProtocolError("Cannot decode response content as JSON"), error)
  File "<string>", line 3, in raise_from
py2neo.errors.ProtocolError: Cannot decode response content as JSON

Does anyone have idea why this error is occuring on my ec2 instance but not on my local machine?

Are you running the python code on the same EC2 instance? Even so, I doubt you can use local host to refer to the EC2 instance. Use the EC2 instances URL instead. See if that helps.

Yeah it's running on the same EC2 instance
It's definitely connecting to the instance correctly

The first line of the response shows that it's established a connection and the credentials are correct

Graph('http://localhost:7474')

That should be the bolt protocol not http. Can you try the neo4j protocol like "neo4j://localhost:7687" as the URL. The error is because the http is sending a browser response which is not expected by the driver which is expecting a json.

1 Like

I'm having a similar issue. Trying to run py2neo code which worked on a remote server on a localhost and getting :

ProtocolError: Cannot decode response content as JSON

If the issue is fixed, can you please mark it as solution?

Thanks

Yes, that fixed it! Thanks so much!

that works! Thx so much!!