node.js - 在 Node.js 中的 Cypher REST API 中向 Neo4j 提供 'query' 参数的正确方法

标签 node.js rest neo4j cypher node-neo4j

如何在node.js中通过POST请求向Neo4j编写正确的Cypher查询?

代码:

var http = require('http');

var options = {
  host: 'a90cfa68c.hosted.neo4j.org',
  port: 7357,
  path: '/db/data/cypher',
  method: 'POST',
  headers: {               
    'Authorization': 'Basic ' + new Buffer("<login>:<password>").toString('base64')                  
  },
  "Content-Type" : "application/json",
  "query" : "START root=node(*) RETURN root" // <--- Doesn't work :(
};

http.request(options, function(res) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
      });
}).end();

结果:

STATUS: 400
HEADERS: {"content-length":"340","content-encoding":"UTF-8","content-type":"application/json","access-control-allow-origin":"*","server":"Jetty(6.1.25)"}
BODY: {
  "message" : "You have to provide the 'query' parameter.",
  "exception" : "BadInputException",
  "stacktrace" : [ "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:63)", "java.lang.reflect.Method.invoke(Method.java:597)", "org.neo4j.server.extension.auth.AuthenticationFilter.doFilter(AuthenticationFilter.java:57)" ]
}

预期结果:
所有 Node 列表(与 Neo4j 控制台中的 POST/db/data/cypher {"query":"START root=node(*) RETURN root"} 相同)

<小时/> 解决方案(感谢安德鲁·帕伦博):

// Changes goes here:
var req = http.request(options, function(res) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      res.setEncoding('utf8');
      res.on('data', function (chunk) {   
      console.log('BODY: ' + chunk);
      });
});
// Writing into body:
req.write('{"query":"START root=node(*) RETURN root"}');
req.end();

最佳答案

您为 http.request 提供了“query”作为无法识别的选项,但您需要将其写入作为请求的正文。您可能还想将“Content-Type”行移动到 headers 选项对象中。

看一下 Node 文档中的示例:

http://nodejs.org/api/http.html#http_http_request_options_callback

关于node.js - 在 Node.js 中的 Cypher REST API 中向 Neo4j 提供 'query' 参数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868727/

相关文章:

javascript - 如果我不想等待 2 个方法而是等待两个方法都完成怎么办

node.js - 具有 23G 可用磁盘空间的 EC2 卷上出现 ENOSPC 错误

java - RESTful Web 服务无法正确处理请求方法

rest - API设计: Caching “partial” nested objects

Neo4j/Cypher Delete with Where "Unknown identifier"

python - 使用 node.js crypto aes256 加密并使用 python2.7 PyCrypto 解密

node.js - 如何在 Lambda 中使用 putParameter 和 aws-sdk for nodejs 获取过去的错误?

javascript - 跨域情况下 javascript 代码无法访问带有 "Location" header 的 XMLHttpRequest POST 响应

graph - 如何优化具有多个节点匹配的 Neo4j Cypher 查询(笛卡尔积)

neo4j - 数据不像 Neo4j 中的搜索那样进入