net/http
require 'net/http'
require 'uri'
so you don't need to manually include thisuri = URI('http://www.blahblah.com')
// same as uri = URI.parse('http://www.blahblah.com')
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP.Get.new uri
response = http.request request # Net::HTTPResponse object
end
.start
creates a connection to an HTTP server and keeps the connection for the duration of the blockPost
requests are similar, just follow posting request
post
requests are made outside the start of the connectionuri = URI("www....")
request = Net::HTTP.Post.new uri.path
// HEADER
request.content_type = "application/json"
// BODY
// dump converts the json to a string
request.body = JSON.dump({
"arg1" => "val1",
"arg2" => "val2",
...
})
uri = "www.google.com/sjf"
Net:HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") do |http|
request = Net::HTTP.Get.new uri
resp = http.request request
end