Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl command equivalent in ruby

I have a curl command that works well, but I need to automate this 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 is working, I had a encoded url that I was passing to URI.escape, that made me post this question about bad response. Foud the issue and fixed it :)

like image 233
user1810502 Avatar asked Jan 30 '14 19:01

user1810502


2 Answers

THE BEST & EASY SOLUTION!!

  1. Copy your CURL code.

  2. Go to this page.

  3. Paste your CURL code.

  4. Be happy.

I tested this solution this page its amazing.

like image 145
uomo_perfetto Avatar answered Sep 17 '22 15:09

uomo_perfetto


you can execute the 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 the back ticks will execute the system curl

like image 44
jtzero Avatar answered Sep 21 '22 15:09

jtzero