Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

grape-on-rack

> 编程语言
Open source

A demo Grape API mounted on RACK.

274 stars0 likes0 views
WebsiteGitHub

About

A demo Grape API mounted on RACK.

Grape API on Rack

A Grape API mounted on Rack.

Run

$ 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 http://127.0.0.1:9292
* 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.

Examples

ping

A hello world example that returns a JSON document.

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

{"ping":"pong"}

post_put

A simple POST and PUT example.

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

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

{"rang":9}

post_json

An example that shows a POST of JSON data.

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

{"reticulated":"lots"}

get_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}]

[{"id":1,"reticulated":false},{"id":2,"reticulated":true}]

rescue_from

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

 
  Unexpected Error

 
  
Ouch...

  
   
  
  

    Something went terribly wrong.
  

 

path_versioning

An example that uses path-based versioning.

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

{"path":"acme"}

header_versioning

An example that uses vendor header-based versioning.

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

{"header":"acme"}

wrap_response

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

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

{"body":{"ping":"pong"},"status":200}

content_type

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

  A red brown fox jumped over the road.

upload_file

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

{"filename":"grape_logo.png","size":4272}
$ 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' }
    end
  end
end

stream_file

An example that streams a file upload/download through an IO-like interface instead of reading the whole file into memory (see upload_file for the in-memory equivalent).

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

HTTP/1.1 201 Created
Content-Type: image/png
Content-Disposition: attachment; filename*=UTF-8''grape_logo.png
Cache-Control: no-cache
Vary: Origin
Transfer-Encoding: chunked

Download the streamed response to a file and verify it's identical to the upload.

$ curl -X POST -F file=@spec/fixtures/grape_logo.png http://localhost:9292/api/download_stream -o grape_logo_downloaded.png
$ cmp spec/fixtures/grape_logo.png grape_logo_downloaded.png && echo "identical"

identical

entites

An example of using grape-entity.

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

{"tool":{"id":"1","length":10,"weight":"20kg"}}

headers

Demonstrates header case-sensitive handling.

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

{"Host":"localhost:9292"}

stream_data

An example of streaming data.

curl http://localhost:9292/api/stream --no-buffer
1
2
3
...

New Relic

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言