HTTP methods
sparqlite supports both GET and POST HTTP methods for SPARQL requests, following the SPARQL 1.1 Protocol specification.
Default methods
Section titled “Default methods”Each query type uses a default HTTP method:
| Query type | Default method | Reason |
|---|---|---|
| SELECT | GET | Read operation, cacheable |
| ASK | GET | Read operation, cacheable |
| CONSTRUCT | GET | Read operation, cacheable |
| DESCRIBE | GET | Read operation, cacheable |
| UPDATE | POST | Write operation (required by spec) |
GET requests pass the query as a URL parameter (?query=...). POST requests pass the query in the request body as form-encoded data.
Overriding the default method
Section titled “Overriding the default method”All read query methods accept a method keyword argument:
from sparqlite import SPARQLClient
with SPARQLClient("https://opencitations.net/meta/sparql") as client: # Use POST for a long SELECT query that may exceed URL length limits result = client.query(long_query, method="POST")
# Use POST for ASK exists = client.ask(query, method="POST")
# Use POST for CONSTRUCT triples = client.construct(query, method="POST")
# Use POST for DESCRIBE data = client.describe(query, method="POST")When to use POST for read queries
Section titled “When to use POST for read queries”Use method="POST" when:
- The query string is very long and may exceed URL length limits (typically 2000-8000 characters depending on the server)
- You want to avoid caching of results
with SPARQLClient("https://opencitations.net/meta/sparql") as client: long_query = "SELECT * WHERE { " + " UNION ".join( f"{{ ?s <http://example.org/p{i}> ?o }}" for i in range(100) ) + " }"
# POST avoids URL length issues result = client.query(long_query, method="POST")