Search code examples
virtuosoopenlink-virtuoso

How can I send SPARQL queries via curl (over HTTP) to Openlink Virtuoso and obtain the results in JSON


I am looking for extensive sparql-graph-crud documentation or other mechanism which details how to submit queries to Openlink Virtuoso SPARQL endpoint.

In short, I would like to learn how to send SPARQL queries (with WHERE clauses) via curl (over HTTP) to Openlink Virtuoso and obtain the results in JSON.

For example, I would like to submit a query such as SELECT * FROM <http://www.example.com/ABC> where { ?s ?p ?o } LIMIT 100 to Openlink Virtuoso at localhost.

The example (below) provided on the OpenLink Virtuoso page is not sufficient for my queries.

curl --verbose --url "http://localhost:8890/sparql-graph-crud?graph-uri=urn:graph:update:test:post"


Solution

  • As described in the Virtuoso documentation, the /sparql-graph-crud endpoint is meant specifically for the SPARQL 1.1 Graph Store HTTP Protocol. If you just want to execute SELECT queries like your sample, you can simply use the /sparql endpoint, with &query= plus a URI-escaped string of your query, and &format=application/rdf+json in the URI to get results as JSON.

    It can be instructional to use your Virtuoso /sparql query form in your web browser (i.e., http://localhost:8890/sparql) to submit your query requesting default (HTML) output, and then adjust the resulting URI just by changing the &format= argument, as --

    curl --verbose --url "http://localhost:8890/sparql-graph-crud?default-graph-uri=&query=SELECT+*+FROM+%3Chttp%3A%2F%2Fwww.example.com%2FABC%3E+where+%7B+%3Fs+%3Fp+%3Fo+%7D+LIMIT+100&format=application/rdf+json&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&run=+Run+Query+"

    Also note that you can load the above URI, having changed only the &query= to &qtxt=, to see the whole form filled out in your browser.

    For quite extensive documentation, there are the Virtuoso website and Virtuoso product manual.

    The OpenLink Community Forum and Virtuoso Users mailing list are also good places to find folks (including many Virtuoso developers) who are particularly knowledgeable about Virtuoso.