Question

I am trying to query my Neo4j database, using the REST API.

I have this working so far without using the Cypher query language to retrieve data, however, now I need to get nodes which are sorted by 'newest' i.e. unix timestamp DESC.

I have tried to add the query within the HTTP body of the NSMutableURLRequest but I am just getting the HTTP error 405.

Here is my code:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@node/%i", EventsManagerDataURL, creatorID]];

NSLog(@"Connecting to URL: %@", url);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *jsonMap = @"{'query' : 'start x  = node(4,6,7) return x order by x.datestart,'params' : {}}";
NSData *jsonData = [jsonMap dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"POST"];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", jsonData.length] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:jsonData];

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

    if (data){
        NSLog(@"%s. Data: %@", _cmd, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    } else {
        NSLog(@"Error: %@", error.localizedDescription);
    }
}];
Was it helpful?

Solution

You're missing a single quote here:

NSString *jsonMap = @"{'query' : 'start x  = node(4,6,7) return x order by x.datestart***'***,'params' : {}}";

And actually, it needs to have double quotes:

NSString *jsonMap = @"{\"query\" : \"start x  = node(4,6,7) return x order by x.datestart\", \"params\" : {}}";

Test with cURL:

curl -H Accept:application/json -H Content-Type:application/json -X POST -d '{"query" : "start x  = node(4,6,7) return x order by x.datestart", "params" : {}}' localhost:7474/db/data/cypher
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top