Ruby net::http: Difference between revisions
From wikinotes
(→GET) |
(→POST) |
||
Line 33: | Line 33: | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="ruby"> | <syntaxhighlight lang="ruby"> | ||
require "net/http" | |||
headers = { "COOKIE": "foo;" } | headers = { "COOKIE": "foo;" } | ||
uri = URI("https://domain.com/path") | uri = URI("https://domain.com/path") |
Latest revision as of 23:16, 12 May 2022
Builtin library to make HTTP requests.
Documentation
official docs https://docs.ruby-lang.org/en/2.7.0/Net/HTTP.html
Usage
GET
require "net/http" headers = { "COOKIE": "foo;" } uri = URI("https://domain.com/path") request = Net::HTTP::Get.new(uri) headers.each { |header, val| request[header] = val } http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = uri.instance_of?(URI::HTTPS) response = http.request(request) puts(response.body)POST
require "net/http" headers = { "COOKIE": "foo;" } uri = URI("https://domain.com/path") request = Net::HTTP::Post.new(uri) request.body = JSON.dump({ query: { foo: "bar" } }) request.content_type = "application/json" # NOTE: cannot just be set in headers headers.each { |header, val| request[header] = val } http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = uri.instance_of?(URI::HTTPS) response = http.request(request) puts(response.body)