Skip to content

augustl/net-http-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5370d43 · Apr 3, 2019

History

35 Commits
Apr 3, 2019
Feb 4, 2016
Dec 2, 2010
Jan 7, 2010
Apr 17, 2011
May 10, 2011
Nov 18, 2014
Jun 23, 2012
Jan 18, 2010
May 12, 2011
Jan 7, 2010
Jul 6, 2010
Apr 26, 2015
Aug 4, 2010
Jan 7, 2010
May 10, 2011
Jan 7, 2010
Jan 7, 2010
Jan 7, 2010

Repository files navigation

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

About

A collection of Ruby Net::HTTP examples.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages