Hi every body, I use rest web service in my rails application for user''s authentication (user creation, login, ...) Can someone explain me, how can i call a REST Web service (not developped in Rails, and deployed by Tomcat) in my rails application via POST method. Thanks in advance for your help -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I use the Ruby Net::HTTP library to construct requests. Best regards, Bill On Mon, Feb 28, 2011 at 10:03 AM, gs84 <salimatag2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi every body, > > I use rest web service in my rails application for user''s > authentication (user creation, login, ...) > Can someone explain me, how can i call a REST Web service (not > developped in Rails, and deployed by Tomcat) in my rails application > via POST method. > > Thanks in advance for your help > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I''ve just used: data = `curl -data xxx url` That doesn''t seem like the "right" way, but it works. On Mon, Feb 28, 2011 at 3:30 PM, Bill Walton <bwalton.im-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I use the Ruby Net::HTTP library to construct requests. > > Best regards, > Bill > > On Mon, Feb 28, 2011 at 10:03 AM, gs84 <salimatag2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi every body, >> >> I use rest web service in my rails application for user''s >> authentication (user creation, login, ...) >> Can someone explain me, how can i call a REST Web service (not >> developped in Rails, and deployed by Tomcat) in my rails application >> via POST method. >> >> Thanks in advance for your help >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 28, 8:03 am, gs84 <salimat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I use rest web service in my rails application for user''s > authentication (user creation, login, ...) > Can someone explain me, how can i call a REST Web service (not > developped in Rails, and deployed by Tomcat) in my rails application > via POST method.You should also read up on ActiveResource. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Define a class like this in your models folder class MyUserWS < ActiveResource::Base self.site = "http://localhost/" # point this to the location of your web service self.format = :json # default format is xml. include this line if you want json self.element_name = "users" # if your class name (MyUserWS in this case) is different from the name of the webservice you are calling, Mention the real name here self.user = "myself-wcPQXmyE0EpBDgjK7y7TUQ@public.gmane.org" # if your webserive need user authentication mention user name and password self.password = "mypassword" end Once you have this... you can instanciate MyUserWS as if it is a model on local database table. You can do all activerecord operations on it as you do on your models Regards Shireesh On Feb 28, 9:03 pm, gs84 <salimat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi every body, > > I use rest web service in my rails application for user''s > authentication (user creation, login, ...) > Can someone explain me, how can i call a REST Web service (not > developped in Rails, and deployed by Tomcat) in my rails application > via POST method. > > Thanks in advance for your help-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 fév, 21:30, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I use the Ruby Net::HTTP library to construct requests. > > Best regards, > Bill >Thanks to all for your answer. Bill, can you show me please how you you use the NET::HTTP library to call web service. Because i have used the NET::HTTP libray like the following, but i alway got 405 error and web service is not called. I have the signup method in my user controller! def signup require "net/http" require "uri" user= User.new(params[:firstname], params[:lastname],params[:password],params[:mobilenumber],params[:email]) uri = URI.parse("myWebServiceUrl") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data({ "password" => "MyPassword","firstname"=>"yourFirstname", "lastname"=>"yourLatsname", "email"=>"email-/E1597aS9LQAvxtiuMwx3w@public.gmane.org","mobilenumber"=>"1234567890"}) response, data = http.request(request) render :text => "#{data}" end Thanks for your help -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, To send data from an action I do this: def send_mydata ... uri = URI.parse(link) net_http = Net::HTTP.new(uri.host, uri.port) net_http.open_timeout = timeout net_http.read_timeout = timeout net_http.use_ssl = (uri.scheme == ''https'')# enable SSL/TLS if net_http.use_ssl? net_http.cert OpenSSL::X509::Certificate.new(File.read(cert_path)) net_http.key = OpenSSL::PKey::RSA.new(File.read(key_path)) net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE end self.response_body = proc do |response, output| net_http.start do |http| http.request_get(uri.request_uri()) do |res| case res when Net::HTTPSuccess then res.read_body do |segment| response.write(segment) end when Net::HTTPRedirection then response.close() unless response.closed? return else raise "Net::HTTPResponse error: #{res.message.to_s()}" end end end end end On Mar 1, 5:50 am, gs84 <salimat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 28 fév, 21:30, Bill Walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I use the Ruby Net::HTTP library to construct requests. > > > Best regards, > > Bill > > Thanks to all for your answer. > Bill, can you show me please how you you use the NET::HTTP library to > call web service. > > Because i have used the NET::HTTP libray like the following, but i > alway got 405 error and web service is not called. > I have the signup method in my user controller! > > def signup > require "net/http" > require "uri" > > user= User.new(params[:firstname], > params[:lastname],params[:password],params[:mobilenumber],params[:email]) > uri = URI.parse("myWebServiceUrl") > http = Net::HTTP.new(uri.host, uri.port) > > request = Net::HTTP::Post.new(uri.request_uri) > request.set_form_data({ "password" => > "MyPassword","firstname"=>"yourFirstname", "lastname"=>"yourLatsname", > "email"=>"em...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org","mobilenumber"=>"1234567890"}) > response, data = http.request(request) > > render :text => "#{data}" > end > > Thanks for your help-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 mar, 19:32, exequiel <efu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > To send data from an action I do this: > > def send_mydata > ... > uri = URI.parse(link) > net_http = Net::HTTP.new(uri.host, uri.port) > net_http.open_timeout = timeout > net_http.read_timeout = timeout > net_http.use_ssl = (uri.scheme == ''https'')# enable SSL/TLS > if net_http.use_ssl? > net_http.cert > OpenSSL::X509::Certificate.new(File.read(cert_path)) > net_http.key = OpenSSL::PKey::RSA.new(File.read(key_path)) > net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE > end > > self.response_body = proc do |response, output| > net_http.start do |http| > http.request_get(uri.request_uri()) do |res| > case res > when Net::HTTPSuccess then > res.read_body do |segment| > response.write(segment) > end > when Net::HTTPRedirection then > response.close() unless response.closed? > return > else > raise "Net::HTTPResponse error: #{res.message.to_s()}" > end > end > end > end > end >Hi, How can i set parameters in this case? Thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, I never tried this, but it should be something like this: required_parameters = ["param1=value1", "param2=value2"] ... self.response_body = proc do |response, output| net_http.start do |http| http.get(uri.path + ''?'' + required_parameters.join(''&'')) case res ... Maybe, you need to "escape" the required parameters. Cheers. On Mar 2, 11:57 am, gs84 <salimat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 mar, 19:32, exequiel <efu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > To send data from an action I do this: > > > def send_mydata > > ... > > uri = URI.parse(link) > > net_http = Net::HTTP.new(uri.host, uri.port) > > net_http.open_timeout = timeout > > net_http.read_timeout = timeout > > net_http.use_ssl = (uri.scheme == ''https'')# enable SSL/TLS > > if net_http.use_ssl? > > net_http.cert > > OpenSSL::X509::Certificate.new(File.read(cert_path)) > > net_http.key = OpenSSL::PKey::RSA.new(File.read(key_path)) > > net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE > > end > > > self.response_body = proc do |response, output| > > net_http.start do |http| > > http.request_get(uri.request_uri()) do |res| > > case res > > when Net::HTTPSuccess then > > res.read_body do |segment| > > response.write(segment) > > end > > when Net::HTTPRedirection then > > response.close() unless response.closed? > > return > > else > > raise "Net::HTTPResponse error: #{res.message.to_s()}" > > end > > end > > end > > end > > end > > Hi, > How can i set parameters in this case? > > Thanks-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Allow me to second the recommendation to read up on ActiveResource - this is exactly what it''s designed for, and in fact if you''re also developing the ReST API, you can use ActiveResource to impose convention (over configuration) in your API - plus you''ll have a nice test rig when you''re done. Only issue is whether enough people in the Ruby community see AR this way and whether it will continue to be maintained. m On Fri, Mar 4, 2011 at 10:42 AM, exequiel <efulet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I never tried this, but it should be something like this: > > required_parameters = ["param1=value1", "param2=value2"] > ... > self.response_body = proc do |response, output| > net_http.start do |http| > http.get(uri.path + ''?'' + required_parameters.join(''&'')) > case res > ... > > Maybe, you need to "escape" the required parameters. > > Cheers. > > On Mar 2, 11:57 am, gs84 <salimat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 1 mar, 19:32, exequiel <efu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi, > > > > > To send data from an action I do this: > > > > > def send_mydata > > > ... > > > uri = URI.parse(link) > > > net_http = Net::HTTP.new(uri.host, uri.port) > > > net_http.open_timeout = timeout > > > net_http.read_timeout = timeout > > > net_http.use_ssl = (uri.scheme == ''https'')# enable SSL/TLS > > > if net_http.use_ssl? > > > net_http.cert > > > OpenSSL::X509::Certificate.new(File.read(cert_path)) > > > net_http.key = OpenSSL::PKey::RSA.new(File.read(key_path)) > > > net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE > > > end > > > > > self.response_body = proc do |response, output| > > > net_http.start do |http| > > > http.request_get(uri.request_uri()) do |res| > > > case res > > > when Net::HTTPSuccess then > > > res.read_body do |segment| > > > response.write(segment) > > > end > > > when Net::HTTPRedirection then > > > response.close() unless response.closed? > > > return > > > else > > > raise "Net::HTTPResponse error: #{res.message.to_s()}" > > > end > > > end > > > end > > > end > > > end > > > > Hi, > > How can i set parameters in this case? > > > > Thanks > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.