Curl task with Ruby Net :: HTTP

I have a bash script that 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?

+4
source share
2 answers

Here's a sample that suppresses SSL validation 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 
+2
source

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

+2
source

Source: https://habr.com/ru/post/1401115/


All Articles