Alex Balgavy

Just some stuff about me.

Here's my dotfiles repository.

Check out my blog.

My keys: PGP, SSH

My crypto wallets (BTC, XMR)


What links here:

HTTP requests with Ruby net/http

POST with custom headers:

require 'net/http'
require 'uri'
require 'json'
uri = URI 'https://example.com'
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true

data = {x: 1, y: 2}
headers = {whatever: true}
resp = https.post('/endpoint', data.to_json, headers)
abort 'Request error' if resp.code_type != Net::HTTPOK

cookies = resp['set-cookie']
body = JSON.load resp.body

GET with query and custom headers:

require 'net/http'
require 'uri'
require 'json'
uri = URI 'https://example.com'
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true

uri.path = '/endpoint'
query = {q: 'yes'}
uri.query = URI.encode_www_form(query)
headers = {whatever: true}
resp = https.get(uri, headers)
abort 'Request error' if resp.code_type != Net::HTTPOK

body = JSON.load resp.body

If you don’t want to verify SSL certs:

@https.verify_mode = OpenSSL::SSL::VERIFY_NONE