SPARQL Protocol for RDF - potential output formats

Looking through the SPARQL Protocol for RDF am I to understand correctly that for a query of the form:

select * where { ?s ?p ?o . }

I can technically (following the standard) only get results using application/sparql-results+xml ?

If I've misunderstood, should I be able to query an endpoint and request alternative formats e.g. JSON?

I've seen it done using the query string, for example: &query=...&output=json

This doesn't seem particularly reliable, should one therefore modify the request header to say something like:

Accept: application/json, application/sparql-results+xml

Any thoughts appreciated.

Hmmm the document does seem to imply that but that is almost never the case for real world endpoints.

Most endpoints will do content negotiation either by:

  1. The HTTP Accept header
  2. Using a special querysting parameter like you've shown

Yes it should be perfectly fine to use an Accept header to specify the formats you are interested in, whether the endpoint parses your header and respects it may be another matter but good implementations should do this.

Virtuoso and 4store I think support json for a SPARQL SELECT and ASK (in the case of Virtuoso). Accepts head for Virtuoso should be:

'application/sparql-results+json'

You should be able to get JSON - see Serializing SPARQL Query Results in JSON. You can do this in TopBraid Suite by creating a Web service that outputs the data in arbitrary text formats. There is an out-of-the-box module that will create JSON for SPARQL results per the aforementioned document.

Using the "Accept" header is a RESTfully clean way to do it, and widely supported among endpoints.