Hello guys, I''m newbie in ruby and Mechanize. I has created a script to make a POST and GET HTTP request to Redmine application. So, everything fine with GET request and authorization. But when I try to do a POST request Mechanize gave a following error: ws-moonshiner# ruby post.rb /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:464:in `post_form'': 500 => Net::HTTPInternalServerError (Mechanize::ResponseCodeError) from /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:357:in `post'' from post.rb:21 Here is my script: require ''rubygems'' require ''mechanize'' require ''logger'' # runnig a Mechanize agent = Mechanize.new { |a| a.user_agent_alias = ''Mac Safari''} agent.log = Logger.new(STDOUT) # get the Redmine login form & fill it out with the username/password page = agent.get("http://127.0.0.1/login") login_form = page.form_with(:action => "/login") login_form.username = ''test'' login_form.password = ''test'' # submit Redmine login form page = agent.submit login_form issues_page = agent.get("http://127.0.0.1/projects/test/issues") agent.post(issues_page, { "priority_id" => "3", ''tracker_id'' => "1", }) Latest message from logger: [...] I, [2010-10-20T13:56:00.340095 #16626] INFO -- : status: 500 I understand that for some reason server generates an internal error. But I have no idea how to fix it. Does anybody know how to resolve this issue? Thank you very much in advance. -- Best regards, Andrew
Hello guys, I''m newbie in Ruby and Mechanize. I has created a script to make a POST and GET HTTP request to Redmine application. So, everything fine with GET request and authorization. But when I try to do a POST request Mechanize gave a following error: ws-moonshiner# ruby post.rb /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:464:in `post_form'': 500 => Net::HTTPInternalServerError (Mechanize::ResponseCodeError) from /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:357:in `post'' from post.rb:21 Here is my script: require ''rubygems'' require ''mechanize'' require ''logger'' # runnig a Mechanize agent = Mechanize.new { |a| a.user_agent_alias = ''Mac Safari''} agent.log = Logger.new(STDOUT) # get the Redmine login form & fill it out with the username/password page = agent.get("http://127.0.0.1/login") login_form = page.form_with(:action => "/login") login_form.username = ''test'' login_form.password = ''test'' # submit Redmine login form page = agent.submit login_form issues_page = agent.get("http://127.0.0.1/projects/test/issues") agent.post(issues_page, { "priority_id" => "3", ''tracker_id'' => "1", }) Latest message from logger: [...] I, [2010-10-20T13:56:00.340095 #16626] INFO -- : status: 500 I understand that for some reason server generates an internal error. But I have no idea how to fix it. Does anybody know how to resolve this issue? Thank you very much in advance. -- Best regards, Andrew
Hi, That looks like an error from the webserver side - http://127.0.0.1/login - not a mechanize issue. Probably worth looking in your server logs... Regards, Chris On 20 October 2010 13:26, Gazizov Andrey <moonshiner at retn.net> wrote:> Hello guys, > > I''m newbie in Ruby and Mechanize. I has created a script to make a POST > and GET HTTP request to Redmine application. So, everything fine with > GET request and authorization. But when I try to do a POST request > Mechanize gave a following error: > > ws-moonshiner# ruby post.rb > /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:464:in > `post_form'': 500 => Net::HTTPInternalServerError > (Mechanize::ResponseCodeError) > from > /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:357:in > `post'' > from post.rb:21 > > Here is my script: > > require ''rubygems'' > require ''mechanize'' > require ''logger'' > > # runnig a Mechanize > agent = Mechanize.new { |a| a.user_agent_alias = ''Mac Safari''} > > agent.log = Logger.new(STDOUT) > > # get the Redmine login form & fill it out with the username/password > page = agent.get("http://127.0.0.1/login") > login_form = page.form_with(:action => "/login") > login_form.username = ''test'' > login_form.password = ''test'' > > # submit Redmine login form > page = agent.submit login_form > > issues_page = agent.get("http://127.0.0.1/projects/test/issues") > > agent.post(issues_page, { > "priority_id" => "3", > ''tracker_id'' => "1", > }) > > Latest message from logger: > [...] > I, [2010-10-20T13:56:00.340095 #16626] INFO -- : status: 500 > > I understand that for some reason server generates an internal error. > But I have no idea how to fix it. Does anybody know how to resolve this > issue? > > Thank you very much in advance. > > -- > Best regards, > Andrew > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101020/181dd752/attachment-0001.html>
To elaborate a little (since I found this out the hard way)... Mechanize only handles HTTP response codes 200 and 302 - others will throw an exception. So what you''re seeing is, as Chris notes, an error on the server side which returns an HTTP 500, which then throws an exception, which isn''t handled and causes the result you''re seeing. Two things you might want to try: 1. Wrap the post in a begin/rescue block so you can handle the exception 2. Use a packet sniffer to analyze the difference between the post request you''re sending through Mechanize and what a browser sends when you submit the form. The server is likely returning 500 due to a malformed request. On Wed, 2010-10-20 at 10:58 -0500, Chris Kimpton wrote:> Hi, > > That looks like an error from the webserver side - > http://127.0.0.1/login - not a mechanize issue. > > Probably worth looking in your server logs... > > Regards, > Chris > > > On 20 October 2010 13:26, Gazizov Andrey <moonshiner at retn.net> wrote: > > Hello guys, > > I''m newbie in Ruby and Mechanize. I has created a script to > make a POST > and GET HTTP request to Redmine application. So, everything > fine with > GET request and authorization. But when I try to do a POST > request > Mechanize gave a following error: > > ws-moonshiner# ruby post.rb > /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:464:in > `post_form'': 500 => Net::HTTPInternalServerError > (Mechanize::ResponseCodeError) > from > /usr/local/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:357:in > `post'' > from post.rb:21 > > Here is my script: > > require ''rubygems'' > require ''mechanize'' > require ''logger'' > > # runnig a Mechanize > agent = Mechanize.new { |a| a.user_agent_alias = ''Mac Safari''} > > agent.log = Logger.new(STDOUT) > > # get the Redmine login form & fill it out with the > username/password > page = agent.get("http://127.0.0.1/login") > login_form = page.form_with(:action => "/login") > login_form.username = ''test'' > login_form.password = ''test'' > > # submit Redmine login form > page = agent.submit login_form > > issues_page > agent.get("http://127.0.0.1/projects/test/issues") > > agent.post(issues_page, { > "priority_id" => "3", > ''tracker_id'' => "1", > }) > > Latest message from logger: > [...] > I, [2010-10-20T13:56:00.340095 #16626] INFO -- : status: 500 > > I understand that for some reason server generates an internal > error. > But I have no idea how to fix it. Does anybody know how to > resolve this > issue? > > Thank you very much in advance. > > -- > Best regards, > Andrew > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20101020/ba01e44f/attachment.html>