Ankit Ladhania
Ankit Ladhania

Reputation: 103

neo4j- node creation (Bad Request) error

i am trying to create a node using REST api jquery ajax i have tried the below code i dont seem to find any error below in syntax so please help. Here is the code:

$.ajax({
            url: "http://localhost:7474/db/data/cypher",
            accepts: "application/json; charset=UTF-8",
            //contentType:"application/json",
            dataType:"json",
            data:{
                "query" : "CREATE (n:Person { props } ) RETURN n",
                "params" : {
                    "props" : {
                          "position" : "Developer",
                          "name" : "Michael",
                          "awesome" : true,
                          "children" : 3
                    }
                }
            },
            type:"POST",
            success:function(data,xhr,status)
            {
                console.log(status);
            },
            error:function(xhr,err,msg){
                console.log(xhr);
                console.log(err);
                console.log(msg);
            }
        });

on execution i get Bad Request error but just before this i executed "start n = node(*) return n" it worked fine so please specify the error... Thanks Alot

Upvotes: 1

Views: 149

Answers (1)

MYK
MYK

Reputation: 845

It seems like he data you are sending is sent as url-parameters, the request you are sending is like:

query=CREATE+(n%3APerson+%7B+props+%7D+)+RETURN+n&params%5Bprops%5D%5Bposition%5D=Developer&params%5Bprops%5D%5Bname%5D=Michael&params%5Bprops%5D%5Bawesome%5D=true&params%5Bprops%5D%5Bchildren%5D=3

but neo4j expects a post request in plain text (not in url-encoded form)

hope you can understand the problem now

EDIT: try this;

var d = { "query" : "CREATE (n:Person { props } ) RETURN n", "params" : { "props" : { "position" : "Developer", "name" : "Michael", "awesome" : true, "children" : 3 } } };
$.ajax({
            url: "http://localhost:7474/db/data/cypher",
            accepts: "application/json; charset=UTF-8",
            contentType:"application/json",
            dataType:"json",
            data:JSON.stringify(d),
            type:"POST",
            success:function(data,xhr,status)
            {
                console.log(status);
            },
            error:function(xhr,err,msg){
                console.log(xhr);
                console.log(err);
                console.log(msg);
            }
        });

Upvotes: 2

Related Questions