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)
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
How to invoke HTTP POST method over SSL in ruby?
You are close, but not quite there. Try something like this instead:
uri = URI.parse("https://auth.api.rackspacecloud.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new("/v1.1/auth")
request.add_field('Content-Type', 'application/json')
request.body = {'credentials' => {'username' => 'username', 'key' => 'key'}}.to_json
response = http.request(request)
This will set the Content-Type header as well as post the JSON in the body, rather than in the form data as your code had it. With the sample credentials, it still fails, but I suspect it should work with real data in there.
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}"
Related Topics
Map Array of Ints to Nested Array Access
How to Install Ruby System-Wide Using Rbenv
Ruby on Rails: Create Confirmation View Before Creating the Object
Return True Only If All Values Evaluate to True in Ruby
Empty Strings at the Beginning and End of Split
Rescuing "Command Not Found" for Io::Popen
What Is the Ruby Equivalent of Preg_Quote()
Using Class Instance Variable for Mutex in Ruby
What Is the Fastest Way to Sort a Hash
Rspec Matcher That Checks Collection to Include Item That Satisfies Lambda
Http.Post_Form in Ruby with Custom Headers
How to Count Duplicates Hash Itens in Ruby 1.8.5 ( Sketchup Ruby API )
How to Add Iedriverserver to Path
Rubocop, How to Disable/Enable Cops on Blocks of Code
Why Is Ruby's Loop Command Slower Than While True
Ruby Code for Modifying Outer Quotes on Strings