【问题标题】:Correctly issuing a POST query via httr for a SPARQL publicly available endpoint通过 httr 为 SPARQL 公开可用的端点正确发出 POST 查询
【发布时间】:2019-06-01 21:36:25
【问题描述】:

我正在尝试使用通过 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 }

【问题讨论】:

  • 我认为,您的 httr POST 请求网址上有一个 .csv 扩展名不应该存在。
  • 真的!我可以想到 TO 在 Web 界面中单击了 "Downloads -> CSV",通常默认为一个名为 sparql.csv 的文件 - 这里的小错字会产生巨大的影响。

标签: r post httprequest sparql httr


【解决方案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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多