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

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"

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.

Following advice from #TallTed, I have composed the following solution.
date;time curl -X POST "http://localhost:8890/sparql" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept:application/sparql-results+json" \
--data-urlencode 'format=json' \
--data-urlencode 'default-graph-uri=http://www.example.com/ABC' \
--data-urlencode 'query=SELECT * FROM <http://www.example.com/ABC> WHERE { ?s ?p ?o } LIMIT 5' \
--write-out '%{url_effective};%{http_code};%{time_total};%{time_namelookup};%{time_connect};%{size_download};%{speed_download}\n';date;

Related

How to use PKI Authentication to use the Opensearch Dashboards API?

I am trying to use OpenSearch Dashboards API (Amazon Kibana fork) to add a search index. I am using version 1.0 and have also setup the security plugin. I have no issues using TLS PKI for interacting with OpenSearch (Elasticsearch). However, whenever I try to interact with the OpenSearch Dashboard API with TLS PKI authentication, I get an error. I am able to setup PKI within Dashboards to authenticate to OpenSearch without any issues. Here is effectively the command on the shell using curl, but I am looking to use python when finished:
curl -X POST --cert MYCERT.crt --key MYKEY.key --cacert MY-CA-BUNDLE.crt https://HOSTNAME:5601/api/saved_objects/index-pattern/test-index -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d' { "attributes": { "title": "test-index-*","timeFieldName": "#timestamp" } }'
I get the following JSON message error back:
{"statusCode":401,"error":"Unauthorized","message":"Authentication required"}
If I instead use the API with 'simple' username/password, the same command works:
curl -X POST -u USERNAME:PASSWORD https://HOSTNAME:5601/api/saved_objects/index-pattern/test-index -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d' { "attributes": { "title": "test-index-*","timeFieldName": "#timestamp" } }'
However, I can't seem to find a way to allow my ansible to issue a command to the API using PKI certificates, which I am looking to do so I can disable simple authentication. Does anyone know what setting I am missing or if I am misusing the API? I have studied the plugin documentation, but I can't find any item that I am missing.
Did you try a simple query to Opensearch service like:
curl -XGET -k https://$(hostname):9200/ --key MYKEY.key --cert MYCERT.crt
If this one is not working, probably you need to check the certificates that are configured in opensearch.yml file.

How to convert Curl to url with headers

I have this command in cURL and it works
curl -X GET \
-H "X-Parse-Application-Id: APP_ID" \
-H "X-Parse-REST-API-Key: API_KEY" \
-G \ https://parseapi.back4app.com/classes/Books
I want create a url that will execute the same way on the browser.
The website that I'm working with is back4app.
There is no way to achieve the same thing with just a URL. This relies on HTTP Headers (both -H parameters) that can't be translated to something else easily. To set these headers in a web browser, you'd at least need to execute JavaScript.
There might be a way if the target API supports reading the same fields from the url in some way (technically, there's no reason not to do this). I haven't found something on that topic in their docs, though.

Academic Knowledge Cognitive Services Labs API error 500

I have obtained API keys for the Cognitive Services Labs by Microsoft here, but when I try using the example provided in the API "Try me" page for the graphSearch method, I get a 500 server error after about 20 seconds. Other API endpoints (interpret, evaluate) seem to be working well. Here's my query (also tried via the "Try me" page):
export SERVER=api.labs.cognitive.microsoft.com
curl "https://${SERVER}/academic/v1.0/graph/search?mode=json" \
-H "Host: ${SERVER}" \
-H 'Content-Type: application/json' \
-H "Ocp-Apim-Subscription-Key: ${API_KEY}" \
--data '{"path":"/paper/AuthorIDs/author","paper":{"type":"Paper","NormalizedTitle":"graph engine","select":["OriginalTitle"]},"author":{"return":{"type":"Author","Name":"bin shao"}}}'
I've written an inquiry via the Cognitive Services UserVoice portal but with no answer so far. The service is not listed in the Azure Status page.
I want to know whether my query or account registration is faulty, or is this a temporary problem. Should be easy to answer.

CURL Command To Create A File On Server

I have a mini program/server built on one of my computers (Machine1) and I am trying to create or overwrite a file through cURL on another computer (Machine2). So Machine2 is connected to Machine1. Ive been looking through cURL's documentation for command that will do this but have had no luck and as well on stack overflow.
https://curl.haxx.se/docs/manpage.html
I have also tried the examples on this SO post:
HTTP POST and GET using cURL in Linux
Any idea as to what the command might be through command prompt? (equivalent of a POST command). I have tried so far using -O, -K, -C and a multitude of others which have not worked.
In command line, all you need to do is using curl --form to simulate a multipart/form-data POST request:
curl --form "testfile=#thefilename.jpg" http://<Machine2>/<Path>
testfile is the field name used for form, if you don't care, just use any english word.
# is used here to make file thefilename.jpg get attached in the post as a file upload. Refer to curl man doc.
In server side, URL http://<Machine2>/<Path> should be listened. When curl send the previous POST request, server side program should get it, extract the attached file (thefilename.jpg), and save to disk.

What is curl doing with -d and -X GET?

I'm looking to this snippet of code:
curl -X GET 'https://api.newrelic.com/v2/applications/1622/metrics/data.json' \
-H 'X-Api-Key:30f4ec24a1f7dd9998a536b05840b17f7d42c7c1' -i \
-d 'names[]=EndUser&names[]=EndUser/Apdex&values[]=call_count&values[]=average_response_time&values[]=score&summarize=true'
from "Listing your app ID and metric data".
But curl's man page only talks about -d/--data in the context of POST requests, so, what's really happening here in terms of the HTTP request sent to the server?
-d with GET request just sends a query string, however the endpoint where data are sent must be set to consume application/x-www-form-urlencoded content type - have just checked that.
In general it's weird and I wouldn't implement it in such a way.
When such query is sent to java servlet - the body is accessible via.. getInputStream() method [sic!].

Resources