• Stars
    star
    1,150
  • Rank 39,172 (Top 0.8 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of Ruby Net::HTTP examples.

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:

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:

RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')

Check out RestClient! https://github.com/rest-client/rest-client

More Repositories

1

halt

OS where everything is immutable! (Experimental)
Rust
273
star
2

ruby-openssl-cheat-sheet

A collection of use cases with examples for Ruby's OpenSSL bindings.
Ruby
246
star
3

js-epub

EPUB library for Javascript
235
star
4

live-validations

No longer maintained.
213
star
5

js-unzip

Unzip files with JavaScript
125
star
6

react-nashorn-example

A poc of rendering a React single page app on the server for the initial page load.
JavaScript
117
star
7

booktorious

Proof of concept JavaScript+web browser EPUB reader
JavaScript
86
star
8

js-inflate

Inflate (uncompress) blobs compressed with the deflate algorithm
82
star
9

ruby-prowl

Ruby interface to Prowl, http://www.prowlapp.com/
Ruby
68
star
10

unobtrusive-google-maps

jQuery plugin. Unobtrusively load interactive Google maps on top of static JPEG Google maps.
32
star
11

concui

Experiment..
Clojure
30
star
12

ansible-playbooks

Collection of my ansible-playbooks for dev setups
Shell
15
star
13

nodejs-sandboxed-fs

A wrapper around the core 'fs' module that allows for sandboxed access by whitelisting or blacklisting
JavaScript
11
star
14

path-travel-agent

Java path matching lib (HTTP, ...)
Java
8
star
15

binbin

Port of some old stuff that contained some (useful?) scripts that some people have linked to from elsewhere
Ruby
7
star
16

augustl.com

HTML
4
star
17

traffic-police

A Ring (++) routing library for Clojure
Clojure
4
star
18

react-native-camera-ios-native-module-issue

A react native app where NativeModules.CameraManager is null, and I don't understand why
Objective-C
2
star
19

dbs-are-fn

The content and code samples for the functional database blog dbs-are-fn.com
Clojure
1
star
20

gradle-warlike-plugin

Groovy
1
star
21

.emacs.d

Emacs Lisp
1
star