Search code examples
curlsparql

How to send a SPARQL cURL request based on a query file?


I have a SPARQL query in a file called test.rq. What is a good way of sending a SPARQL request with cURL that uses the query from that file?

(I'm able to perform such a request with a dedicated SPARQL client or by programming the request by using an HTTP library, but I'm assuming quick command-line request, e.g., with cURL should be doable as well.)

There are three standardized ways for sending a SPARQL request:

Attempt 1: GET

I tried something like the following, but $(VALUE) must come from the file test.rq and it must be URL-encoded.

curl -d 'query=$(VALUE)' $(ENDPOINT)

Approach 2: URL-encoded POST

I tried something like the following, but $(VALUE) should -- again -- come from file test.rq, and URL-encoding should be applied.

curl -X POST -d '$(VALUE)' -H 'Content-Type: application/x-www-form-urlencoded' $(ENDPOINT)

Approach 3: Direct POST

This should have worked without URL encoding, doing something like the following.

curl -X POST -d '@query.rq' -H 'Content-Type: application/sparql-query' $(ENDPOINT)

Unfortunately, most SPARQL endpoints do not support this third approach :(


Solution

  • Case 1 from a file:

    URL encode the contents of the file and put it in the HTTP URL query string as ?query=

    curl --data-urlencode 'query@Q.rq' $(ENDPOINT)