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

Correctly issuing a POST query via httr for a SPARQL publicly available endpoint

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

Option 1: POST (recommended) Issue a POST to the endpoint, with the query in the body, and an Accept header of sparql-results+json:

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

问题

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

  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 } ")

结果return错误响应代码:

 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 }

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")

或(endpoint-specific)

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")

或(endpoint-specific)

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