I am developing an application which uses a common Fuseki dataset with other developers. A Javascript function calls AJAX (jQuery $.ajax()
) and sends to the Python script three variables via POST request.
Javascript:
$.ajax({
url: 'py/registerUser.py',
data: {firstname: registerFirstName, lastname: registerLastName, email: registerEmail},
dataType: 'json',
type: 'post',
success: function(data) {
if(data["result"] == "ok") {
validSPARQL = true;
}
else {
validSPARQL = false;
}
},
error: function(data) {
validSPARQL = false;
}
});
The Python script retrieves the POST variables and manipulates them to create the query string. Then it sets the method to POST and executes the query on the given address.
Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi, cgitb, json, re
from SPARQLWrapper import SPARQLWrapper, JSON
arguments = cgi.FieldStorage()
firstname = arguments.getvalue('firstname')
lastname = arguments.getvalue('lastname')
email = arguments.getvalue('email')
queryString = """PREFIX aop:<http://vitali.web.cs.unibo.it/AnnOtaria/person/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
INSERT DATA {
aop:""" + firstname + lastname + """ a foaf:Person ;
foaf:name \"""" + firstname + """ """ + lastname + """\" ;
foaf:mbox <mailto:""" + email + """> .
}
"""
sparql = SPARQLWrapper("http://linktodataset.com:8181/data/update")
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
sparql.setMethod('POST')
sparql.query()
print "Content-type: text/json\n"
print "{ \"result\": \"ok\" }"
My problem: each time I send the query, I get back a Internal Server Error 500. I tried manipulating the code in many ways, I followed the instructions on this page and had a look on this other one, and many others among which the SPARQLWrapper documentation, but nothing happened.
I tried writing the query string in other ways, imported other libraries. Nothing worked.
Note 1: commenting the line sparql.query()
the error disappears, obviously it doesn't send any query though.
Note 2: I can send SELECT queries to http://linktodataset.com:8181/data/query, but this doesn't work with INSERT DATA.
Note 3: I tried the query string on the Fuseki Control Panel, in the form dedicated to SPARQL Update and it worked just fine. The same query, in the SPARQL Query form, didn't work.
I am sure I'm doing something wrong, but I can't understand what... Any suggestions? Thank you!
EDIT 1 (09-Feb-2015)
I have edited the query string and I tried printing it, this is the result:
PREFIX aop:<http://mypersonallink.com/person/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
INSERT DATA {
aop:TestfirstTestlast a foaf:Person ;
foaf:name "Testfirst Testlast" ;
foaf:mbox <mailto:test@mail.com> .
}
EDIT 2 (10-feb-2015)
I installed a local Fuseki and started it with fuseki-server --update --mem /ds
. The address is localhost:3030
. The Python code has remained almost unchanged (mod_python instead of cgi and, of course, the link to the dataset). Here it is what I get:
MOD_PYTHON ERROR
ProcessId: 13230
Interpreter: '127.0.1.1'
ServerName: '127.0.1.1'
DocumentRoot: '/var/www/personal/html'
URI: '/py/registerUser.py'
Location: None
Directory: '/var/www/personal/html/py/'
Filename: '/var/www/personal/html/py/registerUser.py'
PathInfo: ''
Phase: 'PythonHandler'
Handler: 'mod_python.cgihandler'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/usr/lib/python2.7/dist-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)
File "/var/www/personal/html/py/registerUser.py", line 37, in <module>
sparql.query()
File "/usr/lib/pymodules/python2.7/SPARQLWrapper/Wrapper.py", line 355, in query
return QueryResult(self._query())
File "/usr/lib/pymodules/python2.7/SPARQLWrapper/Wrapper.py", line 330, in _query
raise QueryBadFormed
QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed
Note the last line:
QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed
Well, I commented sparql.query()
and printed the queryString
, copied it and put it into the SPARQL Update
box reachable at http://localhost:3030/sparql.tpl
and it worked just fine.