Search code examples
rubyposthttpsheader

Ruby https POST with headers


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

Solution

  • 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}"