2017-04-03 78 views
0

我正在使用我的节点应用程序中的'请求'模块来将数据置于驻留在fuseki服务器中的本体模型中。我使用下面的代码:通过SPARQL通过来自节点j的HTTP POST请求插入数据

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:当我使用邮递员发送POST请求,将数据插入它工作正常,但是从我的节点的应用程序,它没有。它显示错误'错误请求400'。 P:GET方法从POSTMAN和节点应用都能正常工作。

+0

我的猜测是服务器正在寻找表单数据而不是查询参数,在Postman中,你在发送POST请求时如何发送它?也可能值得在'myquery'上做一个'console.log'来查看它是否正确。 – BenShelton

+0

在Postman中,查询类似于POST方法:: http:// localhost:3030/DS-1 /?update = PREFIX + test%3A%3Chttp%3A%2F%2Fwww.semanticweb.org%2Fmuhammad%2Fontologies %2F2017%2F2%2Funtitled-本体的14%23%3E%0D%0AINSERT +%7B +%3FKPIs +试验%3AhasValue + 11%7D%0D%0AWHERE%0D%0A ++%7B +%3FKPIs +试验%3AhasValue +%3FNewvalue%7D 。 P.s:我已经在节点应用中尝试过这种格式,但它不起作用,并且是'myquery'是正确组成的,我已经检查过它,并且我也尝试过直接将网址像上面那样。 –

回答

0

问题解决了: 我是以发布请求的格式犯了错误。更正后的格式如下。

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 '网址' 的元素。

0

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

INSERT是一个更新操作。

尝试/DS-1/update

最好是张贴更新与内容类型的请求主体。 ?update=可能无法正常工作。

+0

我已经尝试过,但是也出现以下错误:[2017-04-04 11:59:50] Fuseki INFO [48] 404未找到: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%3FKPIs%20test%3A hasValue %202009%7D%20WHERE%20%7B%20%3FKPIs%20test%3AhasValue%20%3FNewvalue%7D' –

+0

注意它现在是“404未找到”。检查服务器的配置以查看是否启用了更新。 ( - 更新,或更新如果使用配置文件)。 – AndyS