Equivalent ruby ​​curl command

I have a curl command that works well, but I need to automate it in a ruby ​​script,

curl cmd:

curl -u usrname:pwd -X POST --data "del=false&val=100" http://localhost:1111/sample/path

I wrote the following code:

uri = URI::HTTPS.build(:host => "localhost", :port => 1111)
uri.path = URI.escape("/sample/path")
client = Net::HTTP.new("localhost", "1111")
req = Net::HTTP::Post.new(uri.request_uri, {"User-Agent" => "UA"})
req.set_form_data({"del" => "false", "val" => "100"})
req.basic_auth("usrname", "pwd")
res = client.request(req)

The above code works, I had a coded URL that I passed in URI.escape, which made me post this question about a bad answer. Fix the problem and fix it :)

+3
source share
3 answers

you can execute curl command directly from ruby

usrname = "username"
pwd = "pwd"
val = 100
del= false
http_path = "http://localhost:1111/sample/path"
puts `curl -u #{usrname}:#{pwd} -X POST --data "del=#{del}&val=#{va}" #{http_path}`

and reverse ticks will perform a curl of the system

+5
source

BEST AND EASY SOLUTION !!

  • Copy the CURL code.

  • Go to the page.

  • Paste your CURL code.

  • Be happy.

I tried this solution on this page, it's awesome.

+5

curb

c = Curl::Easy.new

c.http_auth_types = :basic
c.username = 'usrname'
c.password = 'pwd'

c.http_post("http://localhost:1111/sample/path", "del=false&val=100")
+3

All Articles