curl command equivalent in ruby
15,216
Solution 1
THE BEST & EASY SOLUTION!!
Copy your CURL code.
Go to this page.
Paste your CURL code.
Be happy.
I tested this solution this page its amazing.
Solution 2
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
Solution 3
You can use 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")
Author by
user1810502
Updated on July 27, 2022Comments
-
user1810502 almost 2 years
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 :)
-
user1810502 over 10 yearsI am looking to do a http post in ruby instead of making to do a system call for curl
-
uomo_perfetto over 6 yearsThis is a really good solution, its a applicable solution for this especific question and other group of similars questions. Ill be happy if know the reason for negative votes for my answer.
-
steel over 5 yearsNote: Does not work with all
curl
arguments. No-F
for example. -
Aryeh Beitz almost 5 yearsuse this documentation to find the equivalents of the parameters you need rubydoc.info/github/taf2/curb/Curl/Easy
-
FluorescentGreen5 over 4 yearscurl is platform dependent. also, you shouldn't suggest command line solutions when the language itself has a solution
-
jtzero over 4 yearsThat's an absolute and much like everything else in programming its entirely dependent on context and what you are trying to do.