首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确地通过httr为SPARQL公共可用端点发出POST查询

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

Stack Overflow用户
提问于 2019-06-01 21:36:25
回答 1查看 187关注 0票数 2

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

选项1: POST (推荐)向端点发出一个POST,其中包含正文中的查询,以及sparql-+json的接受头: POST http://statistics.gov.scot/sparql HTTP/1.1主机: statistics.gov.scot接受:statistics.gov.scot/sparql-结果+json内容-类型:application/x表单-urlencoded statistics.gov.scot

问题

我试图以以下方式运行查询,该查询将生成一个具有可用地理位置的表:

代码语言:javascript
复制
  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 } ")

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

代码语言:javascript
复制
 httr::status_code(response)
[1] 400

查询

当对基于web的enpoint接口(https://statistics.gov.scot/sparql-beta)进行测试时,查询工作得很好。

代码语言:javascript
复制
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 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-02 13:56:12

JSON

代码语言:javascript
复制
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")

或(特定端点)

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

CSV

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

或(特定端点)

代码语言:javascript
复制
response <- httr::POST(
    url = "http://statistics.gov.scot/sparql.csv",
    body = list( query = "SELECT * { ?s ?p ?o } LIMIT 10" )
    )
content(response, "text")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56410607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档