Problem Sending POST to Public Endpoint

Previously I have been setting GET requests to the Virtuoso Public Endpoint. It works fine, but sometimes I run into the problem of “Error 414, Query String too long”. So I am considering sending POST requests instead.

The problem is that I am not getting anything back, despite the 200 status code.

Here is what my code looks like:

const data = {
“default-graph-uri”:“http%3A%2F%2Fdbpedia.org”,
“query”:“SELECT+%3Fkey+%3Fval%0D%0AWHERE%7B%0D%0A++++++%3Fkey+dbo%3AareaTotal+%3Fval.%0D%0A++++++VALUES+%3Fkey+%7Bdbr%3ABerlin+dbr%3AToronto+dbr%3AParis%7D%0D%0A%7D%0D%0A”,
“format”:“application%2Fsparql-results%2Bjson”,
“CXML_redir_for_subjs”:“121”,
“CXML_redir_for_hrefs”:"",
“timeout”:“30000”,
“debug”:“on”,
“run”:"+Run+Query_"
}

fetch(“https://dbpedia.org/sparql”, {
method: ‘POST’, // or ‘PUT’
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
},
body: JSON.stringify(data),
}).then((response) => response.json()).then((values) => console.log(values))

I am not sure what I am doing wrong. Any help would be appreciated. Thanks!

Hi @kospades, my best guess is that you put json into the body which you should not, esp. when defining application/x-www-form-urlencoded. @janfo do you have any sample JS code you could paste here?

@kospades best to test with curl . could you send a curl command? Otherwise we can’t verify.

As stated above the problem is the fact you are converting the arguments into a JSON structure which the sparql endpoint does not handle.

As i am not sure exactly what language / tools you were using in your example so i have made a small cUrl example. Note that i removed all the non-essential arguments to the request as in your particular case you only need the query and the format.

curl -v  \
    -d "query=SELECT+%3Fkey+%3Fval%0D%0AWHERE%7B%0D%0A++++++%3Fkey+dbo%3AareaTotal+%3Fval.%0D%0A++++++VALUES+%3Fkey+%7Bdbr%3ABerlin+dbr%3AToronto+dbr%3AParis%7D%0D%0A%7D%0D%0A" \
    -d "format=application%2Fsparql-results%2Bjson" \
    https://dbpedia.org/sparql/

Here is the verbose output:

*   Trying 194.109.129.58...
* TCP_NODELAY set
* Connected to dbpedia.org (194.109.129.58) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* NPN, negotiated HTTP2 (h2)
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Unknown (67):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=dbpedia.org
*  start date: Jun  3 00:51:40 2020 GMT
*  expire date: Sep  1 00:51:40 2020 GMT
*  subjectAltName: host "dbpedia.org" matched cert's "dbpedia.org"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x56229159f410)
> POST /sparql/ HTTP/2
> Host: dbpedia.org
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Length: 210
> Content-Type: application/x-www-form-urlencoded
> 
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
* We are completely uploaded and fine
< HTTP/2 200 
< date: Fri, 31 Jul 2020 18:59:52 GMT
< content-type: application/sparql-results+json
< content-length: 510
< server: Virtuoso/07.20.3235 (Linux) x86_64-generic-linux-glibc25  VDB
< expires: Fri, 07 Aug 2020 18:59:52 GMT
< cache-control: max-age=604800
< access-control-allow-credentials: true
< access-control-allow-methods: GET, POST, OPTIONS
< access-control-allow-headers: Depth,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept-Encoding
< accept-ranges: bytes
< 

And there is the result from your query:

{ "head": { "link": [], "vars": ["key", "val"] },
  "results": { "distinct": false, "ordered": true, "bindings": [
    { "key": { "type": "uri", "value": "http://dbpedia.org/resource/Berlin" }	, "val": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#double", "value": "8.917e+08" }},
    { "key": { "type": "uri", "value": "http://dbpedia.org/resource/Toronto" }	, "val": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#double", "value": "6.3021e+08" }} ] } }