Ruby Https Post with Headers

Ruby https POST with headers

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 'net/https'
require 'json'

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

uri = URI.parse("https:/...")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req['foo'] = 'bar'
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"

Ruby - Send GET request with headers

Using net/http as suggested by the question.

References:

  • Net::HTTP https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html
  • Net::HTTP::get https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#method-c-get
  • Setting headers: https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Setting+Headers
  • Net::HTTP::Get https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP/Get.html
  • Net::HTTPGenericRequest https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html and Net::HTTPHeader https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPHeader.html (for methods that you can call on Net::HTTP::Get)

So, for example:

require 'net/http'    

uri = URI("http://www.ruby-lang.org")
req = Net::HTTP::Get.new(uri)
req['some_header'] = "some_val"

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') { |http|
http.request(req)
}

puts res.body # <!DOCTYPE html> ... </html> => nil

Note: if your response has HTTP result state 301 (Moved permanently), see Ruby Net::HTTP - following 301 redirects

Ruby POST with custom headers and body?

Try this, if it works for you

uri = URI('https://api.mailchimp.com/3.0/lists/xxxx/members/')

Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.basic_auth 'username', 'password'
req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

response = http.request req # Net::HTTPResponse object
end

You need to set form data in post request like

req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

Updates:

Try posting raw data like

json_data = {'from' => '2005-01-01', 'to' => '2005-03-31'}.to_json
req.body = json_data

Ruby - HTTPS get calls with headers

You need to use use_ssl: true.

uri = URI('https://secure.example.com/some_path?query=string')

response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
request['Authentication'] = "Bearer %s" % @token
http.request(request)
end

how do I include a header in an http request in ruby

get_response is a shorthand for making a request, when you need more control - do a full request yourself.

There's an example in ruby standard library here:

uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
req = Net::HTTP::Get.new(uri)
req['token'] = 'fjhKJFSDHKJHjfgsdfdsljh'

res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}

HTTP.post_form in Ruby with custom headers

You can try this, for example:

http = Net::HTTP.new('domain.com', 80)
path = '/url'

data = 'form=data&more=values'
headers = {
'Cookie' => cookie,
'Content-Type' => 'application/x-www-form-urlencoded'
}

resp, data = http.post(path, data, headers)


Related Topics



Leave a reply



Submit