-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathREADME.md
4170 lines (3189 loc) · 116 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
![grape logo](grape.png)
[![Gem Version](https://badge.fury.io/rb/grape.svg)](http://badge.fury.io/rb/grape)
[![Build Status](https://github.com/ruby-grape/grape/workflows/test/badge.svg?branch=master)](https://github.com/ruby-grape/grape/actions)
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape.svg)](https://codeclimate.com/github/ruby-grape/grape)
[![Coverage Status](https://coveralls.io/repos/github/ruby-grape/grape/badge.svg?branch=master)](https://coveralls.io/github/ruby-grape/grape?branch=master)
[![Inline docs](https://inch-ci.org/github/ruby-grape/grape.svg)](https://inch-ci.org/github/ruby-grape/grape)
[![Join the chat at https://gitter.im/ruby-grape/grape](https://badges.gitter.im/ruby-grape/grape.svg)](https://gitter.im/ruby-grape/grape?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## Table of Contents
- [What is Grape?](#what-is-grape)
- [Stable Release](#stable-release)
- [Project Resources](#project-resources)
- [Grape for Enterprise](#grape-for-enterprise)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Rails 7.1](#rails-71)
- [Mounting](#mounting)
- [All](#all)
- [Rack](#rack)
- [Alongside Sinatra (or other frameworks)](#alongside-sinatra-or-other-frameworks)
- [Rails](#rails)
- [Zeitwerk](#zeitwerk)
- [Modules](#modules)
- [Remounting](#remounting)
- [Mount Configuration](#mount-configuration)
- [Versioning](#versioning)
- [Strategies](#strategies)
- [Path](#path)
- [Header](#header)
- [Accept-Version Header](#accept-version-header)
- [Param](#param)
- [Describing Methods](#describing-methods)
- [Configuration](#configuration)
- [Parameters](#parameters)
- [Params Class](#params-class)
- [Declared](#declared)
- [Include Parent Namespaces](#include-parent-namespaces)
- [Include Missing](#include-missing)
- [Evaluate Given](#evaluate-given)
- [Parameter Precedence](#parameter-precedence)
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
- [Supported Parameter Types](#supported-parameter-types)
- [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
- [Custom Types and Coercions](#custom-types-and-coercions)
- [Multipart File Parameters](#multipart-file-parameters)
- [First-Class JSON Types](#first-class-json-types)
- [Multiple Allowed Types](#multiple-allowed-types)
- [Validation of Nested Parameters](#validation-of-nested-parameters)
- [Dependent Parameters](#dependent-parameters)
- [Group Options](#group-options)
- [Renaming](#renaming)
- [Built-in Validators](#built-in-validators)
- [allow_blank](#allow_blank)
- [values](#values)
- [except_values](#except_values)
- [same_as](#same_as)
- [length](#length)
- [regexp](#regexp)
- [mutually_exclusive](#mutually_exclusive)
- [exactly_one_of](#exactly_one_of)
- [at_least_one_of](#at_least_one_of)
- [all_or_none_of](#all_or_none_of)
- [Nested mutually_exclusive, exactly_one_of, at_least_one_of, all_or_none_of](#nested-mutually_exclusive-exactly_one_of-at_least_one_of-all_or_none_of)
- [Namespace Validation and Coercion](#namespace-validation-and-coercion)
- [Custom Validators](#custom-validators)
- [Validation Errors](#validation-errors)
- [I18n](#i18n)
- [Custom Validation messages](#custom-validation-messages)
- [presence, allow_blank, values, regexp](#presence-allow_blank-values-regexp)
- [same_as](#same_as-1)
- [length](#length-1)
- [all_or_none_of](#all_or_none_of-1)
- [mutually_exclusive](#mutually_exclusive-1)
- [exactly_one_of](#exactly_one_of-1)
- [at_least_one_of](#at_least_one_of-1)
- [Coerce](#coerce)
- [With Lambdas](#with-lambdas)
- [Pass symbols for i18n translations](#pass-symbols-for-i18n-translations)
- [Overriding Attribute Names](#overriding-attribute-names)
- [With Default](#with-default)
- [Using dry-validation or dry-schema](#using-dry-validation-or-dry-schema)
- [Headers](#headers)
- [Request](#request)
- [Header Case Handling](#header-case-handling)
- [Response](#response)
- [Routes](#routes)
- [Helpers](#helpers)
- [Path Helpers](#path-helpers)
- [Parameter Documentation](#parameter-documentation)
- [Cookies](#cookies)
- [HTTP Status Code](#http-status-code)
- [Redirecting](#redirecting)
- [Recognizing Path](#recognizing-path)
- [Allowed Methods](#allowed-methods)
- [Raising Exceptions](#raising-exceptions)
- [Default Error HTTP Status Code](#default-error-http-status-code)
- [Handling 404](#handling-404)
- [Exception Handling](#exception-handling)
- [Rescuing exceptions inside namespaces](#rescuing-exceptions-inside-namespaces)
- [Unrescuable Exceptions](#unrescuable-exceptions)
- [Exceptions that should be rescued explicitly](#exceptions-that-should-be-rescued-explicitly)
- [Logging](#logging)
- [API Formats](#api-formats)
- [JSONP](#jsonp)
- [CORS](#cors)
- [Content-type](#content-type)
- [API Data Formats](#api-data-formats)
- [JSON and XML Processors](#json-and-xml-processors)
- [RESTful Model Representations](#restful-model-representations)
- [Grape Entities](#grape-entities)
- [Hypermedia and Roar](#hypermedia-and-roar)
- [Rabl](#rabl)
- [Active Model Serializers](#active-model-serializers)
- [Sending Raw or No Data](#sending-raw-or-no-data)
- [Authentication](#authentication)
- [Basic Auth](#basic-auth)
- [Register custom middleware for authentication](#register-custom-middleware-for-authentication)
- [Describing and Inspecting an API](#describing-and-inspecting-an-api)
- [Current Route and Endpoint](#current-route-and-endpoint)
- [Before, After and Finally](#before-after-and-finally)
- [Anchoring](#anchoring)
- [Instance Variables](#instance-variables)
- [Using Custom Middleware](#using-custom-middleware)
- [Grape Middleware](#grape-middleware)
- [Rails Middleware](#rails-middleware)
- [Remote IP](#remote-ip)
- [Writing Tests](#writing-tests)
- [Writing Tests with Rack](#writing-tests-with-rack)
- [RSpec](#rspec)
- [Airborne](#airborne)
- [MiniTest](#minitest)
- [Writing Tests with Rails](#writing-tests-with-rails)
- [RSpec](#rspec-1)
- [MiniTest](#minitest-1)
- [Stubbing Helpers](#stubbing-helpers)
- [Reloading API Changes in Development](#reloading-api-changes-in-development)
- [Reloading in Rack Applications](#reloading-in-rack-applications)
- [Reloading in Rails Applications](#reloading-in-rails-applications)
- [Performance Monitoring](#performance-monitoring)
- [Active Support Instrumentation](#active-support-instrumentation)
- [endpoint_run.grape](#endpoint_rungrape)
- [endpoint_render.grape](#endpoint_rendergrape)
- [endpoint_run_filters.grape](#endpoint_run_filtersgrape)
- [endpoint_run_validators.grape](#endpoint_run_validatorsgrape)
- [format_response.grape](#format_responsegrape)
- [Monitoring Products](#monitoring-products)
- [Contributing to Grape](#contributing-to-grape)
- [Security](#security)
- [License](#license)
- [Copyright](#copyright)
## What is Grape?
Grape is a REST-like API framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs. It has built-in support for common conventions, including multiple formats, subdomain/prefix restriction, content negotiation, versioning and much more.
## Stable Release
You're reading the documentation for the next release of Grape, which should be 2.3.0.
The current stable release is [2.2.0](https://github.com/ruby-grape/grape/blob/v2.2.0/README.md).
## Project Resources
* [Grape Website](http://www.ruby-grape.org)
* [Documentation](http://www.rubydoc.info/gems/grape)
* Need help? Try [Grape Google Group](http://groups.google.com/group/ruby-grape) or [Gitter](https://gitter.im/ruby-grape/grape)
* [Follow us on Twitter](https://twitter.com/grapeframework)
## Grape for Enterprise
Available as part of the Tidelift Subscription.
The maintainers of Grape are working with Tidelift to deliver commercial support and maintenance. Save time, reduce risk, and improve code health, while paying the maintainers of Grape. Click [here](https://tidelift.com/subscription/request-a-demo?utm_source=rubygems-grape&utm_medium=referral&utm_campaign=enterprise) for more details.
## Installation
Ruby 2.7 or newer is required.
Grape is available as a gem, to install it run:
bundle add grape
## Basic Usage
Grape APIs are Rack applications that are created by subclassing `Grape::API`.
Below is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.
```ruby
module Twitter
class API < Grape::API
version 'v1', using: :header, vendor: 'twitter'
format :json
prefix :api
helpers do
def current_user
@current_user ||= User.authorize!(env)
end
def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
end
resource :statuses do
desc 'Return a public timeline.'
get :public_timeline do
Status.limit(20)
end
desc 'Return a personal timeline.'
get :home_timeline do
authenticate!
current_user.statuses.limit(20)
end
desc 'Return a status.'
params do
requires :id, type: Integer, desc: 'Status ID.'
end
route_param :id do
get do
Status.find(params[:id])
end
end
desc 'Create a status.'
params do
requires :status, type: String, desc: 'Your status.'
end
post do
authenticate!
Status.create!({
user: current_user,
text: params[:status]
})
end
desc 'Update a status.'
params do
requires :id, type: String, desc: 'Status ID.'
requires :status, type: String, desc: 'Your status.'
end
put ':id' do
authenticate!
current_user.statuses.find(params[:id]).update({
user: current_user,
text: params[:status]
})
end
desc 'Delete a status.'
params do
requires :id, type: String, desc: 'Status ID.'
end
delete ':id' do
authenticate!
current_user.statuses.find(params[:id]).destroy
end
end
end
end
```
## Rails 7.1
Grape's [deprecator](https://api.rubyonrails.org/v7.1.0/classes/ActiveSupport/Deprecation.html) will be added to your application's deprecators [automatically](lib/grape/railtie.rb) as `:grape`, so that your application's configuration can be applied to it.
## Mounting
### All
By default Grape will compile the routes on the first route, it is possible to pre-load routes using the `compile!` method.
```ruby
Twitter::API.compile!
```
This can be added to your `config.ru` (if using rackup), `application.rb` (if using rails), or any file that loads your server.
### Rack
The above sample creates a Rack application that can be run from a rackup `config.ru` file with `rackup`:
```ruby
run Twitter::API
```
(With pre-loading you can use)
```ruby
Twitter::API.compile!
run Twitter::API
```
And would respond to the following routes:
GET /api/statuses/public_timeline
GET /api/statuses/home_timeline
GET /api/statuses/:id
POST /api/statuses
PUT /api/statuses/:id
DELETE /api/statuses/:id
Grape will also automatically respond to HEAD and OPTIONS for all GET, and just OPTIONS for all other routes.
### Alongside Sinatra (or other frameworks)
If you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using `Rack::Cascade`:
```ruby
# Example config.ru
require 'sinatra'
require 'grape'
class API < Grape::API
get :hello do
{ hello: 'world' }
end
end
class Web < Sinatra::Base
get '/' do
'Hello world.'
end
end
use Rack::Session::Cookie
run Rack::Cascade.new [Web, API]
```
Note that order of loading apps using `Rack::Cascade` matters. The grape application must be last if you want to raise custom 404 errors from grape (such as `error!('Not Found',404)`). If the grape application is not last and returns 404 or 405 response, [cascade utilizes that as a signal to try the next app](https://www.rubydoc.info/gems/rack/Rack/Cascade). This may lead to undesirable behavior showing the [wrong 404 page from the wrong app](https://github.com/ruby-grape/grape/issues/1515).
### Rails
Place API files into `app/api`. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example, the file name location and directory for `Twitter::API` should be `app/api/twitter/api.rb`.
Modify `config/routes`:
```ruby
mount Twitter::API => '/'
```
#### Zeitwerk
Rails's default autoloader is `Zeitwerk`. By default, it inflects `api` as `Api` instead of `API`. To make our example work, you need to uncomment the lines at the bottom of `config/initializers/inflections.rb`, and add `API` as an acronym:
```ruby
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
```
### Modules
You can mount multiple API implementations inside another one. These don't have to be different versions, but may be components of the same API.
```ruby
class Twitter::API < Grape::API
mount Twitter::APIv1
mount Twitter::APIv2
end
```
You can also mount on a path, which is similar to using `prefix` inside the mounted API itself.
```ruby
class Twitter::API < Grape::API
mount Twitter::APIv1 => '/v1'
end
```
Declarations as `before/after/rescue_from` can be placed before or after `mount`. In any case they will be inherited.
```ruby
class Twitter::API < Grape::API
before do
header 'X-Base-Header', 'will be defined for all APIs that are mounted below'
end
rescue_from :all do
error!({ "error" => "Internal Server Error" }, 500)
end
mount Twitter::Users
mount Twitter::Search
after do
clean_cache!
end
rescue_from ZeroDivisionError do
error!({ "error" => "Not found" }, 404)
end
end
```
## Remounting
You can mount the same endpoints in two different locations.
```ruby
class Voting::API < Grape::API
namespace 'votes' do
get do
# Your logic
end
post do
# Your logic
end
end
end
class Post::API < Grape::API
mount Voting::API
end
class Comment::API < Grape::API
mount Voting::API
end
```
Assuming that the post and comment endpoints are mounted in `/posts` and `/comments`, you should now be able to do `get /posts/votes`, `post /posts/votes`, `get /comments/votes` and `post /comments/votes`.
### Mount Configuration
You can configure remountable endpoints to change how they behave according to where they are mounted.
```ruby
class Voting::API < Grape::API
namespace 'votes' do
desc "Vote for your #{configuration[:votable]}"
get do
# Your logic
end
end
end
class Post::API < Grape::API
mount Voting::API, with: { votable: 'posts' }
end
class Comment::API < Grape::API
mount Voting::API, with: { votable: 'comments' }
end
```
Note that if you're passing a hash as the first parameter to `mount`, you will need to explicitly put `()` around parameters:
```ruby
# good
mount({ ::Some::Api => '/some/api' }, with: { condition: true })
# bad
mount ::Some::Api => '/some/api', with: { condition: true }
```
You can access `configuration` on the class (to use as dynamic attributes), inside blocks (like namespace)
If you want logic happening given on an `configuration`, you can use the helper `given`.
```ruby
class ConditionalEndpoint::API < Grape::API
given configuration[:some_setting] do
get 'mount_this_endpoint_conditionally' do
configuration[:configurable_response]
end
end
end
```
If you want a block of logic running every time an endpoint is mounted (within which you can access the `configuration` Hash)
```ruby
class ConditionalEndpoint::API < Grape::API
mounted do
YourLogger.info "This API was mounted at: #{Time.now}"
get configuration[:endpoint_name] do
configuration[:configurable_response]
end
end
end
```
More complex results can be achieved by using `mounted` as an expression within which the `configuration` is already evaluated as a Hash.
```ruby
class ExpressionEndpointAPI < Grape::API
get(mounted { configuration[:route_name] || 'default_name' }) do
# some logic
end
end
```
```ruby
class BasicAPI < Grape::API
desc 'Statuses index' do
params: (configuration[:entity] || API::Entities::Status).documentation
end
params do
requires :all, using: (configuration[:entity] || API::Entities::Status).documentation
end
get '/statuses' do
statuses = Status.all
type = current_user.admin? ? :full : :default
present statuses, with: (configuration[:entity] || API::Entities::Status), type: type
end
end
class V1 < Grape::API
version 'v1'
mount BasicAPI, with: { entity: mounted { configuration[:entity] || API::Entities::Status } }
end
class V2 < Grape::API
version 'v2'
mount BasicAPI, with: { entity: mounted { configuration[:entity] || API::Entities::V2::Status } }
end
```
## Versioning
You have the option to provide various versions of your API by establishing a separate `Grape::API` class for each offered version and then integrating them into a primary `Grape::API` class. Ensure that newer versions are mounted before older ones. The default approach to versioning directs the request to the subsequent Rack middleware if a specific version is not found.
```ruby
require 'v1'
require 'v2'
require 'v3'
class App < Grape::API
mount V3
mount V2
mount V1
end
```
To maintain the same endpoints from earlier API versions without rewriting them, you can indicate multiple versions within the previous API versions.
```ruby
class V1 < Grape::API
version 'v1', 'v2', 'v3'
get '/foo' do
# your code for GET /foo
end
get '/other' do
# your code for GET /other
end
end
class V2 < Grape::API
version 'v2', 'v3'
get '/var' do
# your code for GET /var
end
end
class V3 < Grape::API
version 'v3'
get '/foo' do
# your new code for GET /foo
end
end
```
Using the example provided, the subsequent endpoints will be accessible across various versions:
```shell
GET /v1/foo
GET /v1/other
GET /v2/foo # => Same behavior as v1
GET /v2/other # => Same behavior as v1
GET /v2/var # => New endpoint not available in v1
GET /v3/foo # => Different behavior to v1 and v2
GET /v3/other # => Same behavior as v1 and v2
GET /v3/var # => Same behavior as v2
```
There are four strategies in which clients can reach your API's endpoints: `:path`, `:header`, `:accept_version_header` and `:param`. The default strategy is `:path`.
### Strategies
#### Path
```ruby
version 'v1', using: :path
```
Using this versioning strategy, clients should pass the desired version in the URL.
curl http://localhost:9292/v1/statuses/public_timeline
#### Header
```ruby
version 'v1', using: :header, vendor: 'twitter'
```
Currently, Grape only supports versioned media types in the following format:
```
vnd.vendor-and-or-resource-v1234+format
```
Basically all tokens between the final `-` and the `+` will be interpreted as the version.
Using this versioning strategy, clients should pass the desired version in the HTTP `Accept` head.
curl -H Accept:application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline
By default, the first matching version is used when no `Accept` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied.
When an invalid `Accept` header is supplied, a `406 Not Acceptable` error is returned if the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
Grape will evaluate the relative quality preference included in Accept headers and default to a quality of 1.0 when omitted. In the following example a Grape API that supports XML and JSON in that order will return JSON:
curl -H "Accept: text/xml;q=0.8, application/json;q=0.9" localhost:1234/resource
#### Accept-Version Header
```ruby
version 'v1', using: :accept_version_header
```
Using this versioning strategy, clients should pass the desired version in the HTTP `Accept-Version` header.
curl -H "Accept-Version:v1" http://localhost:9292/statuses/public_timeline
By default, the first matching version is used when no `Accept-Version` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied and the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
#### Param
```ruby
version 'v1', using: :param
```
Using this versioning strategy, clients should pass the desired version as a request parameter, either in the URL query string or in the request body.
curl http://localhost:9292/statuses/public_timeline?apiver=v1
The default name for the query parameter is 'apiver' but can be specified using the `:parameter` option.
```ruby
version 'v1', using: :param, parameter: 'v'
```
curl http://localhost:9292/statuses/public_timeline?v=v1
## Describing Methods
You can add a description to API methods and namespaces. The description would be used by [grape-swagger][grape-swagger] to generate swagger compliant documentation.
Note: Description block is only for documentation and won't affects API behavior.
```ruby
desc 'Returns your public timeline.' do
summary 'summary'
detail 'more details'
params API::Entities::Status.documentation
success API::Entities::Entity
failure [[401, 'Unauthorized', 'Entities::Error']]
default { code: 500, message: 'InvalidRequest', model: Entities::Error }
named 'My named route'
headers XAuthToken: {
description: 'Validates your identity',
required: true
},
XOptionalHeader: {
description: 'Not really needed',
required: false
}
hidden false
deprecated false
is_array true
nickname 'nickname'
produces ['application/json']
consumes ['application/json']
tags ['tag1', 'tag2']
end
get :public_timeline do
Status.limit(20)
end
```
* `detail`: A more enhanced description
* `params`: Define parameters directly from an `Entity`
* `success`: (former entity) The `Entity` to be used to present the success response for this route.
* `failure`: (former http_codes) A definition of the used failure HTTP Codes and Entities.
* `default`: The definition and `Entity` used to present the default response for this route.
* `named`: A helper to give a route a name and find it with this name in the documentation Hash
* `headers`: A definition of the used Headers
* Other options can be found in [grape-swagger][grape-swagger]
[grape-swagger]: https://github.com/ruby-grape/grape-swagger
## Configuration
Use `Grape.configure` to set up global settings at load time.
Currently the configurable settings are:
* `param_builder`: Sets the [Parameter Builder](#parameters), defaults to `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder`.
To change a setting value make sure that at some point during load time the following code runs
```ruby
Grape.configure do |config|
config.setting = value
end
```
For example, for the `param_builder`, the following code could run in an initializer:
```ruby
Grape.configure do |config|
config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
end
```
You can also configure a single API:
```ruby
API.configure do |config|
config[key] = value
end
```
This will be available inside the API with `configuration`, as if it were [mount configuration](#mount-configuration).
## Parameters
Request parameters are available through the `params` hash object. This includes `GET`, `POST` and `PUT` parameters, along with any named parameters you specify in your route strings.
```ruby
get :public_timeline do
Status.order(params[:sort_by])
end
```
Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and XML content-types.
The request:
```
curl -d '{"text": "140 characters"}' 'http://localhost:9292/statuses' -H Content-Type:application/json -v
```
The Grape endpoint:
```ruby
post '/statuses' do
Status.create!(text: params[:text])
end
```
Multipart POSTs and PUTs are supported as well.
The request:
```
curl --form image_file='@image.jpg;type=image/jpg' http://localhost:9292/upload
```
The Grape endpoint:
```ruby
post 'upload' do
# file in params[:image_file]
end
```
In the case of conflict between either of:
* route string parameters
* `GET`, `POST` and `PUT` parameters
* the contents of the request body on `POST` and `PUT`
Route string parameters will have precedence.
### Params Class
By default parameters are available as `ActiveSupport::HashWithIndifferentAccess`. This can be changed to, for example, Ruby `Hash` or `Hashie::Mash` for the entire API.
```ruby
class API < Grape::API
include Grape::Extensions::Hashie::Mash::ParamBuilder
params do
optional :color, type: String
end
get do
params.color # instead of params[:color]
end
```
The class can also be overridden on individual parameter blocks using `build_with` as follows.
```ruby
params do
build_with Grape::Extensions::Hash::ParamBuilder
optional :color, type: String
end
```
Or globally with the [Configuration](#configuration) `Grape.configure.param_builder`.
In the example above, `params["color"]` will return `nil` since `params` is a plain `Hash`.
Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` and `Grape::Extensions::Hashie::Mash::ParamBuilder`.
### Declared
Grape allows you to access only the parameters that have been declared by your `params` block. It will:
* Filter out the params that have been passed, but are not allowed.
* Include any optional params that are declared but not passed.
* Perform any parameter renaming on the resulting hash.
Consider the following API endpoint:
````ruby
format :json
post 'users/signup' do
{ 'declared_params' => declared(params) }
end
````
If you do not specify any parameters, `declared` will return an empty hash.
**Request**
````bash
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{"user": {"first_name":"first name", "last_name": "last name"}}'
````
**Response**
````json
{
"declared_params": {}
}
````
Once we add parameters requirements, grape will start returning only the declared parameters.
````ruby
format :json
params do
optional :user, type: Hash do
optional :first_name, type: String
optional :last_name, type: String
end
end
post 'users/signup' do
{ 'declared_params' => declared(params) }
end
````
**Request**
````bash
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{"user": {"first_name":"first name", "last_name": "last name", "random": "never shown"}}'
````
**Response**
````json
{
"declared_params": {
"user": {
"first_name": "first name",
"last_name": "last name"
}
}
}
````
Missing params that are declared as type `Hash` or `Array` will be included.
````ruby
format :json
params do
optional :user, type: Hash do
optional :first_name, type: String
optional :last_name, type: String
end
optional :widgets, type: Array
end
post 'users/signup' do
{ 'declared_params' => declared(params) }
end
````
**Request**
````bash
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{}'
````
**Response**
````json
{
"declared_params": {
"user": {
"first_name": null,
"last_name": null
},
"widgets": []
}
}
````
The returned hash is an `ActiveSupport::HashWithIndifferentAccess`.
The `#declared` method is not available to `before` filters, as those are evaluated prior to parameter coercion.
### Include Parent Namespaces
By default `declared(params)` includes parameters that were defined in all parent namespaces. If you want to return only parameters from your current namespace, you can set `include_parent_namespaces` option to `false`.
````ruby
format :json
namespace :parent do
params do
requires :parent_name, type: String
end
namespace ':parent_name' do
params do
requires :child_name, type: String
end
get ':child_name' do
{
'without_parent_namespaces' => declared(params, include_parent_namespaces: false),
'with_parent_namespaces' => declared(params, include_parent_namespaces: true),
}
end
end
end
````
**Request**
````bash
curl -X GET -H "Content-Type: application/json" localhost:9292/parent/foo/bar
````
**Response**
````json
{
"without_parent_namespaces": {
"child_name": "bar"
},
"with_parent_namespaces": {
"parent_name": "foo",
"child_name": "bar"
},
}
````
### Include Missing
By default `declared(params)` includes parameters that have `nil` values. If you want to return only the parameters that are not `nil`, you can use the `include_missing` option. By default, `include_missing` is set to `true`. Consider the following API:
````ruby
format :json
params do
requires :user, type: Hash do
requires :first_name, type: String
optional :last_name, type: String
end
end
post 'users/signup' do
{ 'declared_params' => declared(params, include_missing: false) }
end
````
**Request**
````bash
curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{"user": {"first_name":"first name", "random": "never shown"}}'
````