I''m trying to connect to a remote server in order to post an XML request with payment details. Can connect OK, but can''t seem to get it to log in. I definitely have the correct username and password, since I can log in manually at the URL through my web browser. At the moment, I do get a response back, but it''s a "401 Authorization Required". Have tried lots of other things like using Net::HTTP.Post.new instead, but couldn''t ever seem to get it to work (couldn''t use SSL etc.) - are there any really good examples around of how to do this? Wondered if someone could point out the mistakes in the following code, which is where I''ve arrived at due to NoMethod errors and other problems with the alternative versions I tried: require ''net/https'' def payment limit = 5 @output_text = "" query = render_to_string :action => ''payment'' # Generates the XML to send begin response = '''' site = Net::HTTP.new("secure-test.example.com", 443) site.use_ssl = true headers = {''Content-Type'' => ''text/xml''} response = site.post("/the/url/of/payment/system", query, headers) response.basic_auth "username", "password" #### THE PROBLEM LINE? rescue limit -= 1 if limit > 0 retry else raise "Could not connect to payment server" end end @output_text = response.body rescue RuntimeError => detail flash[:error] = detail end Any help would be greatly appreciated!! Thanks, ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
I know with PHP you would use something along the lines of CURL. Not sure what kind of integration Ruby/Rails has with Curl. - Nathan. -------------------------------------------------------------- Nathaniel S. H. Brown Toll Free 1.877.4.INIMIT Inimit Innovations Phone 604.724.6624 www.inimit.com Fax 604.444.9942> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of > Dave Silvester > Sent: November 1, 2005 10:08 AM > To: Ruby On Rails > Subject: [Rails] Log in to remote site on POST request > > I''m trying to connect to a remote server in order to post an > XML request with payment details. Can connect OK, but can''t > seem to get it to log in. > > I definitely have the correct username and password, since I > can log in manually at the URL through my web browser. At > the moment, I do get a response back, but it''s a "401 > Authorization Required". > > Have tried lots of other things like using Net::HTTP.Post.new > instead, but couldn''t ever seem to get it to work (couldn''t > use SSL etc.) - are there any really good examples around of > how to do this? > > Wondered if someone could point out the mistakes in the > following code, which is where I''ve arrived at due to > NoMethod errors and other problems with the alternative > versions I tried: > > require ''net/https'' > > def payment > limit = 5 > @output_text = "" > query = render_to_string :action => ''payment'' # Generates > the XML to send > begin > response = '''' > site = Net::HTTP.new("secure-test.example.com", 443) > site.use_ssl = true > headers = {''Content-Type'' => ''text/xml''} > response = site.post("/the/url/of/payment/system", query, headers) > response.basic_auth "username", "password" #### THE PROBLEM LINE? > rescue > limit -= 1 > if limit > 0 > retry > else > raise "Could not connect to payment server" > end > end > @output_text = response.body > rescue RuntimeError => detail > flash[:error] = detail > end > > > Any help would be greatly appreciated!! > > Thanks, > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Check out the example from here: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html Here''s the relevant piece: #3: Detailed control url = URI.parse(''http://www.example.com/todo.cgi'') req = Net::HTTP::Post.new(url.path) req.basic_auth ''jack'', ''pass'' req.set_form_data({''from''=>''2005-01-01'', ''to''=>''2005-03-31''}, '';'') res = Net::HTTP.new(url.host, url.port).start { http.request(req) } case res when Net::HTTPSuccess, Net::HTTPRedirection # OK else res.error! end The important steps, it seems to me, in order (Note: I''ve never used this code, but I''ve done stuff in Java that has a similar flow): 1. Create a new Post object 2. Add the basic auth info 3. Set the form data 4. Send the request The code below has some issues: 1. Its setting the headers on no object, so this line is useless 2. Its setting the basic auth information after the request has been sent -----Original Message----- From: Nathaniel S. H. Brown [mailto:nshb-wgYSSEAWXinQT0dZR+AlfA@public.gmane.org] Sent: Tuesday, November 01, 2005 2:30 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails] Log in to remote site on POST request I know with PHP you would use something along the lines of CURL. Not sure what kind of integration Ruby/Rails has with Curl. - Nathan. -------------------------------------------------------------- Nathaniel S. H. Brown Toll Free 1.877.4.INIMIT Inimit Innovations Phone 604.724.6624 www.inimit.com Fax 604.444.9942> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of > Dave Silvester > Sent: November 1, 2005 10:08 AM > To: Ruby On Rails > Subject: [Rails] Log in to remote site on POST request > > I''m trying to connect to a remote server in order to post an > XML request with payment details. Can connect OK, but can''t > seem to get it to log in. > > I definitely have the correct username and password, since I > can log in manually at the URL through my web browser. At > the moment, I do get a response back, but it''s a "401 > Authorization Required". > > Have tried lots of other things like using Net::HTTP.Post.new > instead, but couldn''t ever seem to get it to work (couldn''t > use SSL etc.) - are there any really good examples around of > how to do this? > > Wondered if someone could point out the mistakes in the > following code, which is where I''ve arrived at due to > NoMethod errors and other problems with the alternative > versions I tried: > > require ''net/https'' > > def payment > limit = 5 > @output_text = "" > query = render_to_string :action => ''payment'' # Generates > the XML to send > begin > response = '''' > site = Net::HTTP.new("secure-test.example.com", 443) > site.use_ssl = true > headers = {''Content-Type'' => ''text/xml''} > response = site.post("/the/url/of/payment/system", query, headers) > response.basic_auth "username", "password" #### THE PROBLEM LINE? > rescue > limit -= 1 > if limit > 0 > retry > else > raise "Could not connect to payment server" > end > end > @output_text = response.body > rescue RuntimeError => detail > flash[:error] = detail > end > > > Any help would be greatly appreciated!! > > Thanks, > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Try putting the response.basic_auth line above the post. Basic authentication parameters are sent as part of the request, so maybe try that? Good luck! -DF On 11/2/05, Dave Silvester <dave-AJqNGCqIqVQ7cdpDWioORw@public.gmane.org> wrote:> I''m trying to connect to a remote server in order to post an XML request with > payment details. Can connect OK, but can''t seem to get it to log in. > > I definitely have the correct username and password, since I can log in > manually at the URL through my web browser. At the moment, I do get a > response back, but it''s a "401 Authorization Required". > > Have tried lots of other things like using Net::HTTP.Post.new instead, but > couldn''t ever seem to get it to work (couldn''t use SSL etc.) - are there any > really good examples around of how to do this? > > Wondered if someone could point out the mistakes in the following code, which > is where I''ve arrived at due to NoMethod errors and other problems with the > alternative versions I tried: > > require ''net/https'' > > def payment > limit = 5 > @output_text = "" > query = render_to_string :action => ''payment'' # Generates the XML to send > begin > response = '''' > site = Net::HTTP.new("secure-test.example.com", 443) > site.use_ssl = true > headers = {''Content-Type'' => ''text/xml''} > response = site.post("/the/url/of/payment/system", query, headers) > response.basic_auth "username", "password" #### THE PROBLEM LINE? > rescue > limit -= 1 > if limit > 0 > retry > else > raise "Could not connect to payment server" > end > end > @output_text = response.body > rescue RuntimeError => detail > flash[:error] = detail > end > > > Any help would be greatly appreciated!! > > Thanks, > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi All, After I run generate scaffold ActiveAlert active_alert_admin , the resulting list.rhtml page has this code snippet at its heart: (The database table was named active_alerts.) -- <% for column in ActiveAlert.content_columns %> <td><%=h active_alert.send(column.name) %></td> Can anyone explain the second line above? OK. This seems somewhat opaque to me...I understand ActiveAlert is the ORM object for the db table. But what is active_alert ? Also, I have put a timestamp in one of the table columns, but its output default is uselessly verbose. How would I capture that column''s output and format it in a more parsimonius way? One way I can think of is to write my own format helper and go: myDateFormat(active_alert.send(column.name)) - The helper would regexp test for a DateTime and reformat a DateTime using strformat()... Any help appreciated JB
On Tuesday 01 Nov 2005 22:54, Tom Fakes wrote:> Check out the example from here: > http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.htmlI''d seen this yesterday, and already tried the example you gave, but for some reason it simply didn''t work. From what I remember, it''s because there is no "http" object ever created - who knows where it''s supposed to come from!? The problems with the other examples seemed to come from inability to log in, and overall I found available documentation to be rather thin on the ground. So, after a fair bit of searching around, I finally found this example, which logs in by creating a header rather than using basic_auth... http://www.con-way.com/XML/samplecode/Ruby/xTrackRubyCode.htm ... and modified it to end up with the following working code: require "net/https" def payment username = ''user'' password = ''pass'' paymentXmlHost = ''secure-test.example.com'' paymentXmlUri = ''/url/to/payment/script'' xmlRequest = render_to_string :action => ''payment'' # Prepare the XML view myConnection = Net::HTTP.new(paymentXmlHost,443) myConnection.use_ssl = true limit = 5 @response = "" begin mySession = myConnection.start headers = {} headers[''Content-Type''] = ''text/xml'' headers[''authorization'']=''Basic ''+Base64.b64encode(username+'':''+password) xmlResponse = mySession.request_post(paymentXmlUri, xmlRequest, headers) rescue limit -= 1 if limit > 0 retry else raise "Could not connect to payment server" end end @response = xmlResponse.body rescue RuntimeError => detail flash[:error] = detail end Still tweaking it and tidying it to do what I want it to do, but the above actually works, so hopefully will save someone else wasting time in future! Thanks everyone for the attempts at help though - I have no idea why this was so tricky to do, or why there are apparently so few detailed working examples or docs around! :-S ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/