【问题标题】:Insert data through SPARQL over HTTP POST request from node js通过来自节点 js 的 HTTP POST 请求通过 SPARQL 插入数据
【发布时间】:2017-04-03 14:47:41
【问题描述】:

我在我的节点应用程序中使用“请求”模块在位于 fuseki 服务器中的本体模型中发布数据。我正在使用以下代码:

var request = require('request');
var querystring = require('querystring');

var myquery = querystring.stringify({update: "PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> INSERT { ?KPIs test:hasValue 2009} WHERE { ?KPIs test:hasValue ?Newvalue}"});


request.post('http://localhost:3030/DS-1/sparql?'+myquery, function (error, response, body) {
  if (!error && response.statusCode == 200) {
   // Show the HTML for the Google homepage.
console.log('successful update');
console.log(body);

  } else {
   console.log(response.statusCode);
   console.warn(error);
  }
});

PS:当我使用 POSTMAN 发送 Post 请求以插入数据时,它工作正常,但在我的节点应用程序中,它没有。它显示错误“错误请求 400”。 P.S:GET 方法在 POSTMAN 和节点应用程序中都可以正常工作。

【问题讨论】:

  • 我的猜测是服务器正在寻找表单数据而不是查询参数,在 Postman 中,您如何在工作时发送 POST 请求?也可能值得在myquery 上做一个console.log,看看它是否正确形成。
  • 在 Postman 中,使用 POST 方法 :: localhost:3030/DS-1/… 的查询看起来像这样。 P.s:我在节点应用程序中尝试过这种格式,但它不起作用,是的,'myquery' 格式正确,我已经检查过了,我也尝试过像上面那样直接放置 url。

标签: node.js sparql ontology fuseki http-method


【解决方案1】:

已解决的问题: 我在发布请求的格式上犯了错误。修正后的格式如下。

var request = require('request');
var querystring = require('querystring');

var myquery2 = querystring.stringify({update: "PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> INSERT { ?KPI_Variables test:hasValue_ROB1 2000} WHERE { ?KPI_Variables test:hasValue_ROB1 ?Newvalue FILTER(?KPI_Variables= test:Actual_Production_Time)}"});

request.post({headers: {'content-type' : 'application/x-www-form-urlencoded'},url:'http://localhost:3030/DS-1/?'+myquery2 }, function (error, response, body) {
  if (!error && response.statusCode == 200) {
   // Show the HTML for the Google homepage.
console.log('successful update');
console.log(body);
  } 
  else
  {
   console.log(response.statusCode)
   console.warn(error);
  }
});

我的 request.post 中缺少“headers”和“url”元素。

【讨论】:

    【解决方案2】:

    /DS-1/sparql 是查询服务。

    INSERT 是更新操作。

    试试/DS-1/update

    最好使用 Content-type 在请求正文中发布更新。 ?update= 可能不起作用。

    【讨论】:

    • 我已经尝试过了,但这也给出了以下错误:[2017-04-04 11:59:50] Fuseki INFO [48] 404 Not found: dataset='DS-1' service ='update=PREFIX%20test%3A%3Chttp%3A%2F%2Fwww.semanticweb.org%2Fmuhammad%2Fontol ogies%2F2017%2F2%2Funtitled-ontology-14%23%3E%20INSERT%20%7B%20%3FKPI %20test%3A hasValue%202009%7D%20WHERE%20%7B%20%3FKPIs%20test%3AhasValue%20%3FNewvalue%7D'
    • 注意它现在是“404 Not found”。检查服务器的配置以查看是否启用了更新。 (--update,如果使用配置文件,则更新)。
    猜你喜欢
    • 2014-09-08
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2018-01-16
    • 2020-04-21
    相关资源
    最近更新 更多