2

I try to request query.wikidata.org using curl.

The following command line works perfectly :

$ curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql --data-urlencode query='PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?item ?itemLabel
WHERE {
?item wdt:P31 wd:Q146 . 
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}' -H 'Accept: text/csv'

Since the SPARQL part of the query is long and complex, I would like to store it in an external file (say cats.sparql) and call the file in the curl command line.

This would be cats.sparql :

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemLabel
WHERE {
?item wdt:P31 wd:Q146 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Now, the following command line returns an error :

 $ curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql --data-urlencode query=cats.sparql -H 'Accept: text/csv'curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql --data-urlencode query=cats.sparql -H 'Accept: text/csv'

Here is the beginning of the error :

java.util.concurrent.ExecutionException: org.openrdf.query.MalformedQueryException: Lexical error at line 1, column 12.  Encountered: <EOF> after : "cats.sparql"
2

Ok, I've finally found a solution on this question

$ curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql --data-urlencode query="$(< cats.sparql)" -H 'Accept: text/csv' > wikidata_cats.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.