|
| 1 | +# Migrating from `rest-client` to `Faraday` |
| 2 | + |
| 3 | +The `rest-client` gem is in maintenance mode, and developers are encouraged to migrate to actively maintained alternatives like [`faraday`](https://github.com/lostisland/faraday). This guide highlights common usage patterns in `rest-client` and how to migrate them to `faraday`. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Quick Comparison |
| 8 | + |
| 9 | +| Task | rest-client example | faraday example | |
| 10 | +| ----------------- | -------------------------------------------------------- | -------------------------------------------------------------------------- | |
| 11 | +| Simple GET | `RestClient.get("https://httpbingo.org/get")` | `Faraday.get("https://httpbingo.org/get")` | |
| 12 | +| GET with params | `RestClient.get(url, params: { id: 1 })` | `Faraday.get(url, { id: 1 })` | |
| 13 | +| POST form data | `RestClient.post(url, { a: 1 })` | `Faraday.post(url, { a: 1 })` | |
| 14 | +| POST JSON | `RestClient.post(url, obj.to_json, content_type: :json)` | `Faraday.post(url, obj.to_json, { 'Content-Type' => 'application/json' })` | |
| 15 | +| Custom headers | `RestClient.get(url, { Authorization: 'Bearer token' })` | `Faraday.get(url, nil, { 'Authorization' => 'Bearer token' })` | |
| 16 | +| Get response body | `response.body` | `response.body` | |
| 17 | +| Get status code | `response.code` | `response.status` | |
| 18 | +| Get headers | `response.headers` (returns `Hash<Symbol, String>`) | `response.headers` (returns `Hash<String, String>`) | |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +In your `Gemfile`, replace `rest-client` with: |
| 25 | + |
| 26 | +```ruby |
| 27 | +gem "faraday" |
| 28 | +``` |
| 29 | + |
| 30 | +Then run: |
| 31 | + |
| 32 | +```sh |
| 33 | +bundle install |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Basic HTTP Requests |
| 39 | + |
| 40 | +### GET request |
| 41 | + |
| 42 | +**rest-client:** |
| 43 | + |
| 44 | +```ruby |
| 45 | +RestClient.get("https://httpbingo.org/get") |
| 46 | +``` |
| 47 | + |
| 48 | +**faraday:** |
| 49 | + |
| 50 | +```ruby |
| 51 | +Faraday.get("https://httpbingo.org/get") |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +### GET with Params |
| 57 | + |
| 58 | +**rest-client:** |
| 59 | + |
| 60 | +```ruby |
| 61 | +RestClient.get("https://httpbingo.org/get", params: { id: 1, foo: "bar" }) |
| 62 | +``` |
| 63 | + |
| 64 | +**faraday:** |
| 65 | + |
| 66 | +```ruby |
| 67 | +Faraday.get("https://httpbingo.org/get", { id: 1, foo: "bar" }) |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### POST Requests |
| 73 | + |
| 74 | +**rest-client:** |
| 75 | + |
| 76 | +```ruby |
| 77 | +RestClient.post("https://httpbingo.org/post", { foo: "bar" }) |
| 78 | +``` |
| 79 | + |
| 80 | +**faraday:** |
| 81 | + |
| 82 | +```ruby |
| 83 | +Faraday.post("https://httpbingo.org/post", { foo: "bar" }) |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +### Sending JSON |
| 89 | + |
| 90 | +**rest-client:** |
| 91 | + |
| 92 | +```ruby |
| 93 | +RestClient.post("https://httpbingo.org/post", { foo: "bar" }.to_json, content_type: :json) |
| 94 | +``` |
| 95 | + |
| 96 | +**faraday (manual):** |
| 97 | + |
| 98 | +```ruby |
| 99 | +Faraday.post("https://httpbingo.org/post", { foo: "bar" }.to_json, { 'Content-Type' => 'application/json' }) |
| 100 | +``` |
| 101 | + |
| 102 | +**faraday (with middleware):** |
| 103 | + |
| 104 | +```ruby |
| 105 | +conn = Faraday.new(url: "https://httpbingo.org") do |f| |
| 106 | + f.request :json # encode request body as JSON and set Content-Type |
| 107 | + f.response :json # parse response body as JSON |
| 108 | +end |
| 109 | + |
| 110 | +conn.post("/post", { foo: "bar" }) |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Handling Responses |
| 116 | + |
| 117 | +**rest-client:** |
| 118 | + |
| 119 | +```ruby |
| 120 | +response = RestClient.get("https://httpbingo.org/headers") |
| 121 | +response.code # => 200 |
| 122 | +response.body # => "..." |
| 123 | +response.headers # => { content_type: "application/json", ... } |
| 124 | +``` |
| 125 | + |
| 126 | +**faraday:** |
| 127 | + |
| 128 | +> notice headers Hash keys are stringified, not symbolized like in rest-client |
| 129 | +
|
| 130 | +```ruby |
| 131 | +response = Faraday.get("https://httpbingo.org/headers") |
| 132 | +response.status # => 200 |
| 133 | +response.body # => "..." |
| 134 | +response.headers # => { "content-type" => "application/json", ... } |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Error Handling |
| 140 | + |
| 141 | +**rest-client:** |
| 142 | + |
| 143 | +```ruby |
| 144 | +begin |
| 145 | + RestClient.get("https://httpbingo.org/status/404") |
| 146 | +rescue RestClient::NotFound => e |
| 147 | + puts e.response.code # 404 |
| 148 | +end |
| 149 | +``` |
| 150 | + |
| 151 | +**faraday:** |
| 152 | + |
| 153 | +> By default, Faraday does **not** raise exceptions for HTTP errors (like 404 or 500); it simply returns the response. If you want exceptions to be raised on HTTP error responses, include the `:raise_error` middleware. |
| 154 | +> |
| 155 | +> With `:raise_error`, Faraday will raise `Faraday::ResourceNotFound` for 404s and other exceptions for other 4xx/5xx responses. |
| 156 | +> |
| 157 | +> See also: |
| 158 | +> |
| 159 | +> * [Dealing with Errors](getting-started/errors.md) |
| 160 | +> * [Raising Errors](middleware/included/raising-errors.md) |
| 161 | +
|
| 162 | +```ruby |
| 163 | +conn = Faraday.new(url: "https://httpbingo.org") do |f| |
| 164 | + f.response :raise_error |
| 165 | +end |
| 166 | + |
| 167 | +begin |
| 168 | + conn.get("/status/404") |
| 169 | +rescue Faraday::ResourceNotFound => e |
| 170 | + puts e.response[:status] # 404 |
| 171 | +end |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Advanced Request Configuration |
| 177 | + |
| 178 | +**rest-client:** |
| 179 | + |
| 180 | +```ruby |
| 181 | +RestClient::Request.execute(method: :get, url: "https://httpbingo.org/get", timeout: 10) |
| 182 | +``` |
| 183 | + |
| 184 | +**faraday:** |
| 185 | + |
| 186 | +```ruby |
| 187 | +conn = Faraday.new(url: "https://httpbingo.org", request: { timeout: 10 }) |
| 188 | +conn.get("/get") |
| 189 | +``` |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## Headers |
| 194 | + |
| 195 | +**rest-client:** |
| 196 | + |
| 197 | +```ruby |
| 198 | +RestClient.get("https://httpbingo.org/headers", { Authorization: "Bearer token" }) |
| 199 | +``` |
| 200 | + |
| 201 | +**faraday:** |
| 202 | + |
| 203 | +> Notice headers Hash expects stringified keys. |
| 204 | +
|
| 205 | +```ruby |
| 206 | +Faraday.get("https://httpbingo.org/headers", nil, { "Authorization" => "Bearer token" }) |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## Redirects |
| 212 | + |
| 213 | +**rest-client:** |
| 214 | +Automatically follows GET/HEAD redirects by default. |
| 215 | + |
| 216 | +**faraday:** |
| 217 | +Use the `follow_redirects` middleware (not included by default): |
| 218 | + |
| 219 | +```ruby |
| 220 | +require "faraday/follow_redirects" |
| 221 | + |
| 222 | +conn = Faraday.new(url: "https://httpbingo.org") do |f| |
| 223 | + f.response :follow_redirects |
| 224 | +end |
| 225 | +``` |
0 commit comments