4

I've been trying to get SPARQLWrapper to insert a simple triple into GraphDB with no success. I have no trouble with select queries. Here's Python test code that fails:

db = sparqlw.SPARQLWrapper(endpoint)
query = '''
INSERT {<http://example.com/123456789876> a owl:Thing .}
WHERE {}
'''
db.setQuery(query)
db.method = sparqlw.POST
db.setReturnFormat(sparqlw.JSON)
db.queryType= sparqlw.INSERT
result = db.query()

It returns these errors:

"urllib.error.HTTPError: HTTP Error 400: Bad Request"

and

"SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad 
request has been sent to the endpoint, probably the sparql query is bad 
formed."

Response: b'Missing parameter: query'

I've looked everywhere and tried everything suggested and can't get it to work. Grateful for any good leads.

See my added comment on validation of the query. The suggestion that the question is a duplicate and already answered is not applicable.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • 1
    Possible duplicate of [Sparql Insert Exception : SPARQLWrapper.SPARQLExceptions.QueryBadFormed](https://stackoverflow.com/questions/20636073/sparql-insert-exception-sparqlwrapper-sparqlexceptions-querybadformed) – Jeen Broekstra Sep 18 '17 at 06:04
  • Another piece of information: The query passes validation. Also, the query works inside the GraphDB SPARQL interface, but not through SPARQLWrapper. – John Ambrosiano Sep 18 '17 at 15:42

1 Answers1

2

GraphDB exposes Sesame-style endpoint URLs.
Here below a screenshot of GraphDB 8.3 Workbench help page (I'm using Free Edition).


Help


The following Python code works for me (repositoryID is wikidata):

from SPARQLWrapper import SPARQLWrapper, BASIC

db = SPARQLWrapper("http://localhost:7200/repositories/wikidata/statements")
query = '''
INSERT {<http://example.com/123456789879> a owl:Thing .}
WHERE {}
'''
db.setHTTPAuth(BASIC)
db.setCredentials('login', 'password')
db.setQuery(query)
db.method = "POST"
db.setReturnFormat('json')
db.queryType = "INSERT"
result = db.query()
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • That's encouraging - sort of. What database are you using as your endpoint, and is it behind a firewall or not? – John Ambrosiano Sep 18 '17 at 16:25
  • I'm using my local GraphDB Free Edition installation, I hope it is not behind a firewall. Are you trying to perform update on a public endpoint? – Stanislav Kralin Sep 18 '17 at 16:37
  • When I paste this code in (only changing the endpoint), it fails with the same error: "urllib.error.HTTPError: HTTP Error 400: Bad Request" as before, and "QueryBadFormed". This smells like an HTTP misunderstanding of some kind, but SPARQLWrapper should be handling all the requests in the expected way. Any hunches? – John Ambrosiano Sep 18 '17 at 17:00
  • Perhaps it's helpful to include the proxy support lines. I have to use these to manage the proxy settings. – John Ambrosiano Sep 18 '17 at 17:05
  • `proxy_support = urllib.request.ProxyHandler({})` `opener = urllib.request.build_opener(proxy_support)` `urllib.request.install_opener(opener)` – John Ambrosiano Sep 18 '17 at 17:05
  • Are you having sufficient [privileges](http://graphdb.ontotext.com/documentation/standard/access-rights-and-security.html)? – Stanislav Kralin Sep 18 '17 at 19:49
  • Sufficient for select. Installation of GraphDB out of the box. Presumed admin privileges, but your point is well taken. I'll look into it. – John Ambrosiano Sep 18 '17 at 21:52
  • Cannot find a method in SPARQLWrapper to specify a user and password. – John Ambrosiano Sep 18 '17 at 22:52
  • At this stage wondering if there's an alternative to SPARQLWrapper. – John Ambrosiano Sep 18 '17 at 22:53
  • @JohnAmbrosiano, I have updated my answer. It works for me with authentication. – Stanislav Kralin Sep 19 '17 at 07:55
  • 1
    The .../statements endpoint is the correct convention. I wasn't aware of it, and I didn't recognize it in Mr. Kralin's response. That solves the problem. Thanks, everyone for your help. – John Ambrosiano Sep 19 '17 at 15:17