mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Response With JSON Regardless Of Content Type as a Rails TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1233 TILs and counting..._
|
_1234 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -794,6 +794,7 @@ _1233 TILs and counting..._
|
|||||||
- [Replace An Index With A Unique Index](rails/replace-an-index-with-a-unique-index.md)
|
- [Replace An Index With A Unique Index](rails/replace-an-index-with-a-unique-index.md)
|
||||||
- [Rescue From](rails/rescue-from.md)
|
- [Rescue From](rails/rescue-from.md)
|
||||||
- [Rescue From With A Separate Method](rails/rescue-from-with-a-separate-method.md)
|
- [Rescue From With A Separate Method](rails/rescue-from-with-a-separate-method.md)
|
||||||
|
- [Respond With JSON Regardless of Content Type](rails/respond-with-json-regardless-of-content-type.md)
|
||||||
- [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md)
|
- [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md)
|
||||||
- [Rollback A Specific Migration Out Of Order](rails/rollback-a-specific-migration-out-of-order.md)
|
- [Rollback A Specific Migration Out Of Order](rails/rollback-a-specific-migration-out-of-order.md)
|
||||||
- [Rounding Numbers With Precision](rails/rounding-numbers-with-precision.md)
|
- [Rounding Numbers With Precision](rails/rounding-numbers-with-precision.md)
|
||||||
|
|||||||
33
rails/respond-with-json-regardless-of-content-type.md
Normal file
33
rails/respond-with-json-regardless-of-content-type.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Respond With JSON Regardless Of Content Type
|
||||||
|
|
||||||
|
Let's say you want to serve some JSON from an endpoint (for example, you might
|
||||||
|
be serving the `apple-app-site-association` file for iOS Universal Links).
|
||||||
|
Regardless of whether the endpoint is requested as JSON (`application/json`),
|
||||||
|
HTML (`text/html`), or something else (`plain/text`), you want to respond with
|
||||||
|
JSON.
|
||||||
|
|
||||||
|
The [`format#any`
|
||||||
|
method](https://api.rubyonrails.org/classes/ActionController/MimeResponds.html)
|
||||||
|
can be used when defining the `respond_to` block. This tells the controller
|
||||||
|
that _any_ mimetype is accepted.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
def show
|
||||||
|
respond_to do |format|
|
||||||
|
format.any do
|
||||||
|
render params[:page],
|
||||||
|
formats: 'json',
|
||||||
|
content_type: "application/json",
|
||||||
|
layout: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
The other important element in this is `formats: 'json'` which helps Rails find
|
||||||
|
your `<template_name>.json.erb` file in the `views` directory.
|
||||||
|
|
||||||
|
Though I cannot find any documentation for it, `format.all` appears to work the
|
||||||
|
same as `format.any` as described in the above example.
|
||||||
|
|
||||||
|
h/t Dillon Hafer
|
||||||
Reference in New Issue
Block a user