View on GitHub


Rack middleware for Linked Data content negotiation.

Download this project as a .zip file Download this project as a tar.gz file

Linked Data Content Negotiation for Rack Applications

This is Rack middleware that provides Linked Data content negotiation for Rack applications. You can use Rack::LinkedData with any Ruby web framework based on Rack, including with Ruby on Rails 3.0 and with Sinatra.

Gem Version Build Status



Adding Linked Data content negotiation to a Rails 3.x application

# config/application.rb
require 'rack/linkeddata'

class Application < Rails::Application
  config.middleware.use Rack::LinkedData::ContentNegotiation

Adding Linked Data content negotiation to a Sinatra application

#!/usr/bin/env ruby -rubygems
require 'sinatra'
require 'rack/linkeddata'

use Rack::LinkedData::ContentNegotiation

get '/hello' do do |graph|
    graph << [, RDF::DC.title, "Hello, world!"]

Adding Linked Data content negotiation to a Rackup application

#!/usr/bin/env rackup
require 'rack/linkeddata'

rdf = do |graph|
  graph << [, RDF::DC.title, "Hello, world!"]

use Rack::LinkedData::ContentNegotiation
run lambda { |env| [200, {}, rdf] }

Defining a default Linked Data content type

use Rack::LinkedData::ContentNegotiation, :default => "text/turtle"

Options are also passed to the writer, which can allow options to be shared among the application and different components.

shared_options = {:default => "text/turtle", :standard_prefixes => true, }
use Rack::LinkedData::ContentNegotiation, shared_options
run MyApplication, shared_options

Testing Linked Data content negotiation using rackup and curl

$ rackup doc/examples/

$ curl -iH "Accept: text/plain" http://localhost:9292/hello
$ curl -iH "Accept: text/turtle" http://localhost:9292/hello
$ curl -iH "Accept: application/rdf+xml" http://localhost:9292/hello
$ curl -iH "Accept: application/json" http://localhost:9292/hello
$ curl -iH "Accept: application/trix" http://localhost:9292/hello
$ curl -iH "Accept: */*" http://localhost:9292/hello


Rack::LinkedData implements content negotiation for any Rack response object that implements the RDF::Enumerable mixin. You would typically return an instance of RDF::Graph or RDF::Repository from your Rack application, and let the Rack::LinkedData::ContentNegotiation middleware take care of serializing your response into whatever RDF format the HTTP client requested and understands.

The middleware queries RDF.rb for the MIME content types of known RDF serialization formats, so it will work with whatever serialization plugins that are currently available for RDF.rb. (At present, this includes support for N-Triples, N-Quads, Turtle, RDF/XML, RDF/JSON, JSON-LD, RDFa, TriG and TriX.)





The recommended installation method is via RubyGems. To install the latest official release of the gem, do:

% [sudo] gem install rack-linkeddata


To get a local working copy of the development repository, do:

% git clone git://

Alternatively, download the latest development version as a tarball as follows:

% wget


This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.




This is free and unencumbered public domain software. For more information, see or the accompanying {file:UNLICENSE} file.