Ruby https POST with headers
Asked Answered
S

5

37

How can I make a Https post with a header in Ruby with a json?

I have tried:

uri = URI.parse("https://...")
    https = Net::HTTP.new(uri.host,uri.port)
    req = Net::HTTP::Post.new(uri.path)
    req['foo'] = bar
    res = https.request(req)
puts res.body
Stripy answered 29/6, 2012 at 21:30 Comment(0)
S
65

The problem it was a json. This solve my problem. Anyway, my question was not clear, so the bounty goes to Juri

require 'uri'
require 'net/http'
require 'json'

@toSend = {
    "date" => "2012-07-02",
    "aaaa" => "bbbbb",
    "cccc" => "dddd"
}.to_json

uri = URI("https:/...")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
req['foo'] = 'bar'
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"
Stripy answered 2/7, 2012 at 20:56 Comment(3)
The code somehow didn't work for me. Instead of @toSend={}.to_json, I had to do req.set_form_data(@toSend) to properly send my data. Hopefully this will help out someone else who got stuck.Wilk
I didn't need to use HTTPS but found a working solution with custom headers here: https://mcmap.net/q/425715/-http-post_form-in-ruby-with-custom-headersGoneness
it should be initheader: {'Content-Type' =>'application/json'}Blitzkrieg
A
40

Try:

require 'net/http'
require 'net/https'

uri = URI.parse("https://...")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
req['foo'] = bar
res = https.request(req)
puts res.body
Architrave answered 2/7, 2012 at 16:55 Comment(3)
how do I set the body and the headers?Kooima
req.body = "The body"Old
https.use_ssl = true was what is needed if you are connecting to https serverLuteolin
A
18

Here's a cleaner way to use Net::HTTP. If you just want to get the response and throw other objects away this is quite useful.

require 'net/http'
require 'json'

uri = URI("https://example.com/path")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/json'
  # The body needs to be a JSON string, use whatever you know to parse Hash to JSON
  req.body = {a: 1}.to_json
  http.request(req)
end
# The "res" is what you need, get content from "res.body". It's a JSON string too.
Atmo answered 11/12, 2015 at 9:18 Comment(1)
Beautiful! Didn't know you could pass in use_ssl as an attribute! Thank you!Witting
T
11

A secure-by-default example:

require 'net/http'
require 'net/https'

req = Net::HTTP::Post.new("/some/page.json", {'Content-Type' =>'application/json'})
req.body = your_post_body_json_or_whatever
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1 # ruby >= 2.0 supports :TLSv1_1 and :TLSv1_2.
# SSLv3 is broken at time of writing (POODLE), and it's old anyway.

http.verify_mode = OpenSSL::SSL::VERIFY_PEER # please don't use verify_none.

# if you want to verify a server is from a certain signing authority, 
# (self-signed certs, for example), do this:
http.ca_file = 'my-signing-authority.crt'
response = http.start {|http| http.request(req) }
Thames answered 16/10, 2014 at 6:0 Comment(2)
how do I set the body and headers?Kooima
{'Content-Type' =>'application/json'} is the hash that becomes the headers. "your_post_body_json_or_whatever" is your body.Thames
P
2

Its working, you can pass data and header like this:

header = {"some" => "header"}
data = {"a" => "123"}
uri = URI("https://anyurl.com")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri, header)
req.body = data.to_json
res = https.request(req)

puts "Response #{res.code} #{res.message}: #{res.body}"
Pierro answered 13/6, 2016 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.