0

我正在尝试使用 REST API 查询我的 Neo4j 数据库。

到目前为止,我没有使用 Cypher 查询语言来检索数据,但现在我需要获取按“最新”排序的节点,即 unix 时间戳 DESC。

我试图在 HTTP 正文中添加查询,NSMutableURLRequest但我只是收到 HTTP 错误405

这是我的代码:

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);
    }
}];
4

1 回答 1

3

您在这里缺少单引号:

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

实际上,它需要有双引号:

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

使用 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
于 2013-01-23T17:08:42.640 回答