Search code examples
rposthttprequestsparqlhttr

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


I'm trying to source some publicly available data using SPARQL endpoint offered via statistics.gov.scot. The API page recommends using 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

Problem

I'm trying to run query that would produce a table with available geographies in the following manner:

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

The results return a code for a wrong response:

 httr::status_code(response)
[1] 400

Query

The query works fine when tested against the web based enpoint interface (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 }

Solution

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

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

    or (endpoint-specific)

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