Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/augustl/net-http-cheat-sheet
A collection of Ruby Net::HTTP examples.
https://github.com/augustl/net-http-cheat-sheet
Last synced: 14 days ago
JSON representation
A collection of Ruby Net::HTTP examples.
- Host: GitHub
- URL: https://github.com/augustl/net-http-cheat-sheet
- Owner: augustl
- License: mit
- Created: 2010-01-07T08:42:55.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2019-04-03T04:53:58.000Z (over 5 years ago)
- Last Synced: 2024-10-15T12:08:21.234Z (28 days ago)
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 1,148
- Watchers: 42
- Forks: 163
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Ruby `Net::HTTP` cheat sheet
A bunch of examples of various use cases, implemented with Ruby's `Net::HTTP` library.
# Alternatives to `Net::HTTP`
This cheat sheet was created many years ago, when invoking `Net::HTTP` directly was common. These days, there are better alternatives around, with much nicer APIs.
Compare multipart file uploads with `Net::HTTP`:
```ruby
BOUNDARY = "AaB03x"uri = URI.parse("http://something.com/uploads")
file = "/path/to/your/testfile.txt"post_body = []
post_body << "--#{BOUNDARY}\r\n"
post_body << "Content-Disposition: form-data; name=\"datafile\"; filename=\"#{File.basename(file)}\"\r\n"
post_body << "Content-Type: text/plain\r\n"
post_body << "\r\n"
post_body << File.read(file)
post_body << "\r\n--#{BOUNDARY}--\r\n"http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.body = post_body.join
request["Content-Type"] = "multipart/form-data, boundary=#{BOUNDARY}"http.request(request)
```And file uploads with RestClient - just a single line, and no shoddy manual string concatenation:
```ruby
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
```
Check out RestClient! https://github.com/rest-client/rest-client