Search code examples
restsparqlgraphdb

How run delete queries via GraphDB REST API


I'm trying to run DELETE queries from the GraphDB REST API with Python requests module, without luck.

SELECT queries with requests.get() work fine:

query = "http://localhost:7200/repositories/myreponame?name=&infer=true&sameAs=false&query=SELECT%20%3Fs%20%3Fp%20%3Fo%0AWHERE%20%7B%20%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0A"
response = requests.get(query, headers = {"Authorization" : token})

But whenever I switch to a DELETE query, it won't work.

If I use the DELETE query with requests.get() I get this error:

query = "http://localhost:7200/repositories/myreponame?name=&infer=true&sameAs=false&query=DELETE%20%7B%0A%20%20%20%20%09%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0AWHERE%20%7B%20%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0A"

MALFORMED QUERY: Encountered " "delete" "DELETE "" at line 1, column 1.
Was expecting one of:
    "base" ...
    "prefix" ...
    "select" ...
    "construct" ...
    "describe" ...
    "ask" ...

If I use the DELETE query with requests.delete() instead, I get:

Repository delete error: query supplied with request

What am I doing wrong?

I'm accessing the API with admin privileges, so that is not the issue.

EDIT: Based on UninformedUser's comment I tried the switching to a POST request, and modifying the URL from query to update. Also added the appropriate content type as described here:

query = "http://localhost:7200/repositories/myreponame?name=&infer=true&sameAs=false&update=DELETE%20%7B%0A%20%20%20%20%09%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0AWHERE%20%7B%20%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0A"
response = requests.post(query, headers = {"Authorization" : token, "Content-Type": "application/x-www-form-urlencoded"})

Now getting the following error:

Missing parameter: query

Solution

  • Turns out that I was missing a specific requirement from GraphDB: for updates it's necessary to append /statements right after the repository ID:

    query = "http://localhost:7200/repositories/myreponame/statements?name=&infer=true&sameAs=false&update=DELETE%20%7B%0A%20%20%20%20%09%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0AWHERE%20%7B%20%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%0A"
    response = requests.post(query, headers = {"Authorization" : token, "Content-Type": "application/x-www-form-urlencoded"})