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.htmlNet::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.htmlNet::HTTPGenericRequest
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html andNet::HTTPHeader
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPHeader.html (for methods that you can call onNet::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 res
ponse 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
How to Compare Strings Ignoring the Case
Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby
"Whenever" Gem Running Cron Jobs on Heroku
Redefining a Single Ruby Method on a Single Instance with a Lambda
Using Join Tables in Ruby on Rails
How to Check If a Value Is a Number
What Does the "$" Character Mean in Ruby
Error: SASS Installation for Windows
How to Configure Ruby on Rails with Oracle
How to Count the Number of Records That Have a Unique Value in a Particular Field in Ror
How to Run a Single Test in Minitest
Welcome/Home Page in Ruby on Rails - Best Practice
Ubuntu Rails Install Fails on Zlib
How to Install Rvm System Requirements Without Giving Sudo Access for Rvm User
How to Test a File Upload in Rails