You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Params on a `namespace` (or whatever alias you are using) also work when using
2499
-
`before_validation` or `after_validation`:
2502
+
Params on a `namespace` (or whichever alias you are using) will also be available when using `before_validation` or `after_validation`:
2500
2503
2501
2504
```ruby
2502
2505
classMyAPI < Grape::API
@@ -2523,6 +2526,8 @@ GET /123 # 'Fixnum'
2523
2526
GET /foo # 400 error - 'blah is invalid'
2524
2527
```
2525
2528
2529
+
**Versioning**
2530
+
2526
2531
When a callback is defined within a version block, it's only called for the routes defined in that block.
2527
2532
2528
2533
```ruby
@@ -2556,6 +2561,33 @@ GET /foo/v1 # 'v1-hello'
2556
2561
GET /foo/v2 # 'v2-hello'
2557
2562
```
2558
2563
2564
+
**Altering Responses**
2565
+
2566
+
Using `present` in any callback allows you to add data to a response:
2567
+
2568
+
```ruby
2569
+
classMyAPI < Grape::API
2570
+
format:json
2571
+
2572
+
after_validation do
2573
+
present :name, params[:name] if params[:name]
2574
+
end
2575
+
2576
+
get '/greeting'do
2577
+
present :greeting, 'Hello!'
2578
+
end
2579
+
end
2580
+
```
2581
+
2582
+
The behaviour is then:
2583
+
2584
+
```bash
2585
+
GET /greeting # {"greeting":"Hello!"}
2586
+
GET /greeting?name=Alan # {"name":"Alan","greeting":"Hello!"}
2587
+
```
2588
+
2589
+
Instead of altering a response, you can also terminate and rewrite it from any callback using `error!`, including `after`. This will cause all subsequent steps in the process to not be called. **This includes the actual api call and any callbacks**
2590
+
2559
2591
## Anchoring
2560
2592
2561
2593
Grape by default anchors all request paths, which means that the request URL
0 commit comments