简体   繁体   中英

Neo4j, Cypher merge query does not execute with REST API

I want to use REST API (.NET) and to issue a merge query to execute for neo4j database. My query is:

{ "query" : "MERGE  (n:Person { props } ) ON CREATE SET n.id = 55 RETURN n" , "params" :{  "props" : { "name" : 'Mahsa', "lastname" : 'Hassankashi' }  }}

OR

{ "query" : "MERGE  (n:Person { props } ) ON CREATE SET n.id = {PersonID}  RETURN n" , "params" :{  "props" : { "name" : 'Mahsa' }   , "PersonID" : 55} }

But my "HttpWebResponse response" is null and when HttpWebResponse returns null because when I send it to stream it at this line:

Stream requestStream = request.GetRequestStream();

It has Length and position error:

(1): 'requestStream.Position' threw an exception of type 'System.NotSupportedException'

(2). "This stream does not support seek operations.", it means my query is not proper. How can I correct query.

It is important that I have answer from my httpwebresponse for other cypher query, it means that my sending request(cypher) and receiving response is possible by other queries.

Below Query works when I put it directly on neo4j 2.2.4:

MERGE  (n:Person { name : "mahsa", lastname : "hassankashi" } ) ON CREATE SET n.id=55 RETURN n

Request Class:

//****** Send Cypher Query by HttpWebRequest and Stream
            HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(url);
            request.KeepAlive = true;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Method = methodType;
            request.Timeout = Timeout.Infinite;


            byte[] postBytes = Encoding.UTF8.GetBytes(json);

            request.ContentType = "application/json; charset=UTF-8";
            request.Accept = "application/json";
            request.ContentLength = postBytes.Length;

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            response = (HttpWebResponse)request.GetResponse();

Thank you in advanced for help.

I used Cypher Transnational Endpoint without using parameters, when I am using parameters I cannot use Merge with REST API.

{"statements" : [ {"statement" : "MERGE (n:Person { name : 'Mahsa' , lastname : 'Hassankashi' } ) ON CREATE SET n.id =1 RETURN n"} ]}

POST to : http://localhost:7474/db/data/transaction/commit

Looks like that you use invalid JSON format for Cypher transactional endppoint

In your case this should be something like this:

{
  "statements" : [ {
    "statement" : "MERGE  (n:Person { props } ) ON CREATE SET n.id = 55 RETURN n",
    "parameters" : {
      "props" : {
        "name" : 'Mahsa', 
        "lastname" : 'Hassankashi'
      }
    }
  } ]
}

This will be correct JSON input for transaction endpoint.

If you use some specific .NET library for making calls to Neo4j database - you should verify your input against their documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM