Skip to content

Commit 616ad92

Browse files
authored
Update Kittens API lesson to use HTTParty instead of RestClient (#30501)
1 parent caf8a20 commit 616ad92

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ruby_on_rails/apis/project_kittens_api.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ This is a fast and straightforward project where you'll set up a Rails app to be
7474

7575
1. Now it's time to make the Kittens resource available via API.
7676

77-
1. Open a new command line tab and fire up IRB. We'll use `rest-client` gem to send requests to our app:
77+
1. Add the HTTParty gem to your project and open a Rails console. We'll use the `HTTParty` gem to send requests to our app:
78+
79+
```bash
80+
bundle add httparty
81+
rails console
82+
```
83+
84+
Then in the console:
7885

7986
```ruby
80-
require 'rest-client' # If you get an error here, you most likely need to install the gem.
81-
response = RestClient.get("http://localhost:3000/kittens")
87+
response = HTTParty.get("http://localhost:3000/kittens")
8288
```
8389

8490
1. Let's see what we got back:
@@ -90,18 +96,18 @@ This is a fast and straightforward project where you'll set up a Rails app to be
9096
```
9197

9298
If you check out your server output, it's probably processing as \*/\* (i.e. all media types), e.g. `Processing by KittensController#index as */*`
93-
1. Try asking specifically for a JSON response by adding the option `accept: :json`, e.g.:
99+
1. Try asking specifically for a JSON response by adding the headers option:
94100

95101
```ruby
96-
json_response = RestClient.get("http://localhost:3000/kittens", accept: :json)
102+
json_response = HTTParty.get("http://localhost:3000/kittens", headers: { 'Accept' => 'application/json' })
97103
```
98104

99105
You most likely will get a 406 Not Acceptable error - check your server console and you will see ActionController talking about UnknownFormat for your controller.
100106
1. Now modify your KittenController's `#index` method to `#respond_to` JSON and render the proper variables.
101-
1. Test it out by making sure your RestClient calls return the proper JSON strings, e.g.:
107+
1. Test it out by making sure your HTTParty calls return the proper JSON strings, e.g.:
102108

103109
```ruby
104-
json_response = RestClient.get("http://localhost:3000/kittens", accept: :json)
110+
json_response = HTTParty.get("http://localhost:3000/kittens", headers: { 'Accept' => 'application/json' })
105111
puts json_response.body
106112
```
107113

0 commit comments

Comments
 (0)