Thursday 15 April 2010

Ruby example of Net::HTTP for GET, POST, PUT, DELETE -



Ruby example of Net::HTTP for GET, POST, PUT, DELETE -

i'm trying larn ruby first time. have experience in php , in php, made function like

function call_api(endpoint,method,arr_parameters='') { // curl phone call }

which utilize like

call_api('https://api.com/user','get','param=1&param=2'); call_api('https://api.com/user/1','get'); call_api('https://api.com/user/1','post','param=1&param=2'); call_api('https://api.com/user/1','put','param=1&param=2'); call_api('https://api.com/user/1','delete');

so far, i've learned how , post phone call ruby so:

conn = net::http.new(api_url, api_port) resppost = conn.post("/user", 'param=1', {}) respget = conn.get("/user?param=1",{})

but don't know how delete , put. can show sample code delete , set calls net::http object?

you namespace it:

net::http::put.new(uri)

same delete:

net::http::delete.new(uri)

you can existing calls:

conn = net::http.new(uri) con.get(path)

that equivalent to:

net::http::get.new(uri)

ruby net-http

No comments:

Post a Comment