Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl on Ruby on Rails

how to use curl on ruby on rails? Like this one

curl -d 'params1[name]=name&params2[email]' 'http://mydomain.com/file.json'
like image 969
Lian Avatar asked Apr 04 '13 06:04

Lian


People also ask

What is curl in Ruby?

curl-to-ruby is a tool to instantly convert curl commands to ruby code using net/http in the browser. It does not guarantee high-fidelity conversions, but it's good enough for most API docs that have curl samples.

What does curl command do?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.


1 Answers

Just in case you don't know, it requires 'net/http'

require 'net/http'

uri = URI.parse("http://example.org")

# Shortcut
#response = Net::HTTP.post_form(uri, {"user[name]" => "testusername", "user[email]" => "[email protected]"})

# Full control
http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"user[name]" => "testusername", "user[email]" => "[email protected]"})

response = http.request(request)
render :json => response.body

Hope it'll helps others.. :)

like image 121
Lian Avatar answered Oct 24 '22 23:10

Lian