Skip to content
A collection of Ruby Net::HTTP examples.
Latest commit ce3aabe Feb 4, 2016 @augustl Fix syntax highlighting

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:


uri = URI.parse("")
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 <<
post_body << "\r\n--#{BOUNDARY}--\r\n"

http =, uri.port)
request =
request.body = post_body.join
request["Content-Type"] = "multipart/form-data, boundary=#{BOUNDARY}"


And file uploads with RestClient - just a single line, and no shoddy manual string concatenation: '/data', :myfile =>"/path/to/image.jpg", 'rb')

Check out RestClient!

Something went wrong with that request. Please try again.