SPARQL POST requests result in 500 Internal server for the same query as GET

53 views
Skip to first unread message

Barry Pearce

unread,
Jul 14, 2022, 6:37:30 AM7/14/22
to Getty Vocabularies as Linked Open Data
Hi folks,

This is probably an implementation thing, but I cannot get the POST method to work either from curl nor from Java.

The endpoint I am using is: http://vocab.getty.edu/sparql.jsonld

I have a query that works great when using the GET method:

PREFIX tgn: <http://vocab.getty.edu/tgn/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX gvp: <http://vocab.getty.edu/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <http://schema.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX term_flag: <http://vocab.getty.edu/term/flag/>

CONSTRUCT {
?s ?p1 ?o1.  # subject
?sn ?p2 ?o2. # note
?n ?p3 ?o3. # names
?pl ?p4 ?o4. # place
?st ?p5 ?o5. # place types
}
WHERE {
    BIND (tgn:7011179 as ?s)
    {?s ?p1 ?o1 FILTER(!isBlank(?o1)
        && (?p1 in (dc:identifier, gvp:prefLabelGVP, skosxl:prefLabel, skosxl:altLabel, skos:scopeNote,
                    gvp:broaderPreferred, gvp:placeTypeNonPreferred, gvp:placeTypePreferred))
        && ?s!=?o1) }
    UNION {?s skos:scopeNote ?sn. ?sn ?p2 ?o2
            FILTER(?p2 in (dc:identifier, rdf:value)) }
    UNION {?s gvp:prefLabelGVP ?n. ?n ?p3 ?o3
            FILTER(?p3 in (dc:identifier, gvp:term, skosxl:literalForm, gvp:historicFlag, gvp:termFlag, term_flag:Vernacular)) }
    UNION {?s skosxl:prefLabel ?n. ?n ?p3 ?o3
            FILTER(?p3 in (dc:identifier, gvp:term, skosxl:literalForm, gvp:historicFlag, gvp:termFlag, term_flag:Vernacular)) }
    UNION {?s skosxl:altLabel ?n. ?n ?p3 ?o3
            FILTER(?p3 in (dc:identifier, gvp:term, skosxl:literalForm, gvp:historicFlag, gvp:termFlag, term_flag:Vernacular)) }
    UNION {?s foaf:focus ?pl. ?pl ?p4 ?o4
            FILTER(?p4 in (wgs:lat, wgs:long)) }
    UNION {?st rdf:subject ?s ;
            rdf:predicate gvp:placeTypeNonPreferred.
            ?st ?p5 ?o5 FILTER(?p5 in (rdf:object, rdf:predicate, gvp:historicFlag)) }
    UNION {?st rdf:subject ?s ;
            rdf:predicate gvp:placeTypePreferred.
            ?st ?p5 ?o5 FILTER(?p5 in (rdf:object, rdf:predicate, gvp:historicFlag)) }
}

(Attached as a file for convenience).

If I urlencode this and place on the URL using "?query=" and use GET everything works great (allowing for the charset in the response issue I have raised as another conversation).

However, I cannot get this same query to work with POST.

Testing with curl: 
curl -v -H "Content-Type: application/sparql-query" -H "Accept: application/ld+json;charset=UTF-8" -d @sparql http://vocab.getty.edu/sparql.jsonld

Results in a 500 internal server error. However the body is in UTF-8, and the SPARQL spec suggests this should be uuenocded.

So I uuencoded the file and I get the same result. 

Any pointers to where I am going wrong with the POST is appreciated.

Cheers
Barry
sparql

Getty Vocabularies LOD

unread,
Jul 14, 2022, 10:57:41 AM7/14/22
to Getty Vocabularies as Linked Open Data
Hi, Barry.

I was able to get a response by urlencoding the query parameter, using this curl statement:

$ curl -X POST -H "Accept: application/ld+json" --data-urlencode "query@sparql" http://vocab.getty.edu/sparql

As another example, we use the Python requests library, which takes care of a lot of this configuration, to get the turtle format for a construct statement:

             "http://vocab.getty.edu/sparql",
            data={"query": construct_query},
            headers={"Accept": "text/turtle", "Accept-Charset": "utf-8"},
)

So the query parameter needs to be set in the POST data.

Hope this helps.

Gregg

Barry Pearce

unread,
Jul 14, 2022, 11:15:48 AM7/14/22
to Getty Vocabularies as Linked Open Data
HI Gregg,

That's good to know - which appears to be the method in 2.1. 2 "query via POST with URL-encoded parameters". That is very useful, I assume the python sets the Content-Type for the query to "application/x-www-form-urlencoded", as required by the spec as I suspect curl does due to the -data-urlencode argument.

In modern internet with 8-bit transfer being standard along with UTF-8 and un-encoded JSON, encoding anything is somewhat outdated. I am therefore trying to avoid the waste of encoding something that really does not need to be encoded.

In 2.1.3 "query via POST directly" of the SPARQL protocol standard it says that one should be able to POST directly (it says "Protocol clients may send protocol requests via the HTTP POST method by including the query directly and unencoded as the HTTP request message body". The Content-Type in this case should be set to "application/sparql-query". 

It is this method I am trying to use (I incorrectly tried uuencode in a subsequent test after failing the unencoded test because I mis-read the word - so we can ignore my comments about uuencoding!!

According to the protocol spec: curl -v -H "Content-Type: application/sparql-query" -H "Accept: application/ld+json;charset=UTF-8" -d @sparql http://vocab.getty.edu/sparql.jsonld
should work - does the server not accept direct POSTs?

Cheers
Barry

Getty Vocabularies LOD

unread,
Jul 14, 2022, 11:42:45 AM7/14/22
to Getty Vocabularies as Linked Open Data
Apparently, the server does not comply with the updated spec. We can look into updating it.
Gregg

Vladimir Alexiev

unread,
Jul 27, 2022, 3:42:07 AM7/27/22
to Getty Vocabularies as Linked Open Data
Barry, Gregg's posting as `query@sparql`, but you are posting just `@sparql`. So your POST data is missing the field name `query`.

Reply all
Reply to author
Forward
0 new messages