Kir
Kir

Reputation: 8111

curl task with Ruby Net::HTTP

I have a bash script which use curl:

url="https://example.com/api.cgi"
message="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<request>
    <encoding>utf-8</encoding>
    <format>XML</format>
    <foo>bar</foo>
</request>"

curl --data "${message}" --header 'Content-Type: text/xml' "${url}" --insecure -3

How to implement the same with ruby Net::HTTP?

Upvotes: 4

Views: 3928

Answers (2)

Roland Mai
Roland Mai

Reputation: 31077

Here's a sample which suppresses SSL verification if you are using self signed certificates.

require "net/http"
require "uri"

uri = URI.parse("https://mysite.com/api.cgi")

message="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<request>
    <encoding>utf-8</encoding>
    <format>XML</format>
    <foo>bar</foo>
</request>"

http = Net::HTTP.new(uri.host, uri.port)
#http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(uri.request_uri)
request.content_type = "text/xml"
request.body = message
response = http.request(request)

p response.body

Upvotes: 3

Said Kaldybaev
Said Kaldybaev

Reputation: 9952

Here's a great Cheat Sheet from Peter Cooper about Ruby Net::HTTP, take a look ! http://www.rubyinside.com/nethttp-cheat-sheet-2940.html

Upvotes: 2

Related Questions