通过httr为SPARQL公开可用端点正确发出POST查询

时间:2019-06-01 21:36:25

标签: r post httprequest sparql httr

我正在尝试使用通过statistics.gov.scot提供的SPARQL端点来获取一些公开可用的数据。 API page建议使用POST。

  

选项1:POST(推荐)   发出POST到端点,并在正文中添加查询,并使用sparql-results + json的Accept头:

POST http://statistics.gov.scot/sparql HTTP/1.1 Host:
statistics.gov.scot Accept: application/sparql-results+json
Content-Type: application/x-www-form-urlencoded
query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

问题

我正在尝试运行查询,该查询将通过以下方式生成具有可用地理位置的表:

  response <- httr::POST(
    url = "http://statistics.gov.scot/sparql.csv",
    query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>  
             SELECT  ?hierarchy ?label 
                WHERE {   ?hierarchy  
                           rdfs:subPropertyOf     
                              <http://statistics.gov.scot/def/hierarchy/best-fit>  ;  
                            rdfs:label ?label } ")

结果返回错误响应的代码:

 httr::status_code(response)
[1] 400

查询

针对基于Web的Enpoint界面(https://statistics.gov.scot/sparql-beta)进行测试时,该查询工作正常。

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>  
SELECT  ?hierarchy  ?label 
 WHERE {   
  ?hierarchy  
  rdfs:subPropertyOf  <http://statistics.gov.scot/def/hierarchy/best-fit>  ;
  rdfs:label ?label }

1 个答案:

答案 0 :(得分:1)

JSON

response <- httr::POST(
    url = "http://statistics.gov.scot/sparql", accept("application/sparql-results+json"),
    body = list( query = "SELECT * { ?s ?p ?o } LIMIT 10" )
    )
content(response, "text")

或(特定于端点)

response <- httr::POST(
    url = "http://statistics.gov.scot/sparql.json",
    body = list( query = "SELECT * { ?s ?p ?o } LIMIT 10" )
    )
content(response, "text")

CSV

response <- httr::POST(
    url = "http://statistics.gov.scot/sparql", accept("text/csv"),
    body = list( query = "SELECT * { ?s ?p ?o } LIMIT 10" )
    )
content(response, "text")

或(特定于端点)

response <- httr::POST(
    url = "http://statistics.gov.scot/sparql.csv",
    body = list( query = "SELECT * { ?s ?p ?o } LIMIT 10" )
    )
content(response, "text")
相关问题