|
| 1 | +# Fetch API |
| 2 | + |
| 3 | +Ruby's Net::HTTP is very powerful, but has a complicated API. OpenURI is easy to use, but has limited functionality. Third-party HTTP clients each have different APIs, and it can sometimes be difficult to learn how to use them. |
| 4 | + |
| 5 | +There is one HTTP client that we can all use. It is the browser's Fetch API. This is an HTTP client for Ruby that can be used in a way that is similar to the Fetch API. |
| 6 | + |
| 7 | +This is not intended to be a complete copy of the Fetch API. For example, responses are returned synchronously rather than asynchronously, as that is more familiar behavior for Ruby programmers. It is only "like" the Fetch API. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Install the gem and add to the application's Gemfile by executing: |
| 12 | + |
| 13 | +``` |
| 14 | +$ bundle add fetch-api |
| 15 | +``` |
| 16 | + |
| 17 | +If bundler is not being used to manage dependencies, install the gem by executing: |
| 18 | + |
| 19 | +``` |
| 20 | +$ gem install fetch-api |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Basic |
| 26 | + |
| 27 | +``` ruby |
| 28 | +require 'fetch-api' |
| 29 | + |
| 30 | +res = Fetch::API.fetch('https://example.com') |
| 31 | + |
| 32 | +# res is a Rack::Response object |
| 33 | +puts res.body |
| 34 | +``` |
| 35 | + |
| 36 | +``` ruby |
| 37 | +require 'fetch-api' |
| 38 | + |
| 39 | +include Fetch::API |
| 40 | + |
| 41 | +res = fetch('https://example.com') |
| 42 | +``` |
| 43 | + |
| 44 | +### Post JSON |
| 45 | + |
| 46 | +``` ruby |
| 47 | +res = fetch('http://example.com', **{ |
| 48 | + method: 'POST', |
| 49 | + |
| 50 | + headers: { |
| 51 | + 'Content-Type' => 'application/json' |
| 52 | + }, |
| 53 | + |
| 54 | + body: { |
| 55 | + name: 'Alice' |
| 56 | + }.to_json |
| 57 | +}) |
| 58 | +``` |
| 59 | + |
| 60 | +### Post Form |
| 61 | + |
| 62 | +``` ruby |
| 63 | +res = fetch('http://example.com', **{ |
| 64 | + method: 'POST', |
| 65 | + |
| 66 | + headers: { |
| 67 | + 'Content-Type' => 'multipart/form-data' |
| 68 | + }, |
| 69 | + |
| 70 | + body: Rack::Multipart.build_multipart( |
| 71 | + file: Rack::Multipart::UploadedFile.new(io: StringIO.new('foo'), filename: 'foo.txt') |
| 72 | + ) |
| 73 | +}) |
| 74 | +``` |
| 75 | + |
| 76 | +Note: `Rack::Multipart.build_multipart` returns nil if the parameter does not include UploadedFile. |
| 77 | + |
| 78 | +## Development |
| 79 | + |
| 80 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 81 | + |
| 82 | +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 83 | + |
| 84 | +## Contributing |
| 85 | + |
| 86 | +Bug reports and pull requests are welcome on GitHub at https://github.com/ursm/fetch-api. |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments