Skip to content


Repository files navigation

Grape API on Rack

Test Code Climate

A Grape API mounted on Rack.


$ bundle install
$ rackup

Loading NewRelic in developer mode ...
Puma starting in single mode...
* Puma version: 6.4.0 (ruby 2.7.7-p221) ("The Eagle of Durango")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 82944
* Listening on
* Listening on http://[::1]:9292

List Routes

rake routes

Explore the API

Explore the API using Swagger UI. Run the application and navigate to http://locahost:9292/swagger/index.html.


A hello world example that returns a JSON document.

$ curl http://localhost:9292/api/ping


A simple POST and PUT example.

curl -XPOST -d '' http://localhost:9292/api/ring 

curl -XPUT -d '{"count":2}' -H "Content-Type:application/json" http://localhost:9292/api/ring 


An example that shows a POST of JSON data.

$ curl -XPOST http://localhost:9292/api/spline -d '{"reticulated":"lots"}' -H "Content-Type:application/json"


An example that pre-processes params sent as JSON data.

$ curl http://localhost:9292/api/reticulated_splines?splines=[{"id":1,"reticulated":true},{"id":2,"reticulated":false}]


An example of rescue_from that wraps all exceptions in an HTTP error code 500.

$ curl -i http://localhost:9292/api/raise
HTTP/1.1 500 Internal Server Error
Last-Modified: Wed, 15 Jun 2022 01:12:37 GMT
Content-Type: text/html
Content-Length: 234
Vary: Origin
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 01:51:25 GMT
Connection: Keep-Alive

  <title>Unexpected Error</title>
  <a href="">
   <img src="/images/rack-logo.png">
    Something went terribly wrong.

An example that uses path-based versioning.

$ curl http://localhost:9292/api/vendor


An example that uses vendor header-based versioning.

$ curl -H "Accept:application/vnd.acme-v1+json" http://localhost:9292/api


A middleware that wraps all responses and always returns HTTP code 200.

$ curl http://localhost:9292/api/decorated/ping


An example that overrides the default Content-Type or returns data in both JSON and XML formats.

$ curl http://localhost:9292/api/plain_text

A red brown fox jumped over the road.
$ curl http://localhost:9292/api/mixed

{"data":"A red brown fox jumped over the road."}
$ curl http://localhost:9292/api/mixed.xml

<?xml version="1.0" encoding="UTF-8"?>
  <data>A red brown fox jumped over the road.</data>

An example that demonstrates a file upload and download.

$ curl -X POST -i -F image_file=@spec/fixtures/grape_logo.png http://localhost:9292/api/avatar

$ curl -X POST -i -F file=@spec/fixtures/grape_logo.png http://localhost:9292/api/download.png

HTTP/1.1 201 Created
Content-Type: image/png
Content-Disposition: attachment; filename*=UTF-8''grape_logo.png
Vary: Origin
Content-Length: 4272
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 02:12:21 GMT
Connection: Keep-Alive
$ curl -X POST -i -F file=@api/ping.rb http://localhost:9292/api/download.rb

HTTP/1.1 201 Created
Content-Type: application/x-ruby
Content-Disposition: attachment; filename*=UTF-8''ping.rb
Vary: Origin
Content-Length: 115
Server: WEBrick/1.4.2 (Ruby/2.6.5/2019-10-01)
Date: Sat, 18 Jun 2022 02:12:47 GMT
Connection: Keep-Alive

module Acme
  class Ping < Grape::API
    format :json
    get '/ping' do
      { ping: 'pong' }

An example of using grape-entity.

$ curl http://localhost:9292/api/entities/1


Demonstrates header case-sensitive handling.

$ curl http://localhost:9292/api/headers/Host


An example of streaming data.

curl http://localhost:9292/api/stream --no-buffer

New Relic

The application is setup with NewRelic w/ Developer Mode. Navigate to http://localhost:9292/newrelic after making some API calls.


A demo Grape API mounted on RACK.







No releases published

Sponsor this project



No packages published