srdst13 has asked for the wisdom of the Perl Monks concerning the following question:

I have just started using WWW::Mechanize, so this is a very basic problem, undoubtedly. I have the following code that queries a web service. It is a pretty straightforward query of this page, primer3.
# URL for primer3 my $url = 'http://frodo.wi.mit.edu/cgi-bin/primer3/primer3_www.cgi'; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get($url); # set up input to form $mech->field('SEQUENCE','ACAGCNTCTGTTGACTCCCCTGCCAGCAAGCCCACTGGCCTGAGC +ACATGAAGTCCTGCACCCAGTCAGGAGAAGGAGCGCTCCTGGCACCAGCAGGAGCTGGCAAAGGCTCTG +GAGAGCTTAGAAAGGGAAAAAATGGAGCTGGAAATGAGGCTAAAGGAGCAGCAGACAGAAATGGAGGCC +ATCCAGGCCCAGAGGGAAGAAGAACGGACCCAGGCAGAGAGTGCCCTATGCCAGATGCAGCTGGAAACA +GAGAAGGAGAGAGTATCCCTCCTGGAGACACTGCTGCAGACGCAGAAGGAGCTAGCAGATGCCAGCCAA +CAACTGGACGGAACTGAGGCAA'); $mech->field('PRIMER_NUM_RETURN',1); # just for debugging purposes print $mech->content(); # Get response by clicking submit button my $resp = $mech->click('Pick Primers'); print "\n\n******* AND Response *********\n\n"; print $resp->content();
No problems except that the "click" method produces the result from the results page:
Did not see the 'Pick Primers' query parameter at /usr/local/apache2/c +gi-bin/primer3/primer3_www_results.cgi line 117 main::main() called at /usr/local/apache2/cgi-bin/primer3/prim +er3_www_results.cgi line 105
I can't figure out why this is the case. The web page works fine interactively and the CGI script that handles the response does a simple check like
if ($query->param('Pick Primers')) { process_input($query); } else { confess "Did not see the 'Pick Primers' query parameter" }
Any help would be much appreciated.

Sean

Update: Does this have anything to do with having a space in the parameter name/value? I have tried this code with a couple of other websites without problems--the only difference seems to be the presence of the space in the parameter name 'Pick Primers'.

Replies are listed 'Best First'.
Re: WWW::Mechanize and submit button question
by marto (Cardinal) on Aug 08, 2005 at 13:08 UTC
    Hi,

    try something like this:
    #!/usr/bin/perl use WWW::Mechanize; # URL for primer3 my $url = 'http://frodo.wi.mit.edu/cgi-bin/primer3/primer3_www.cgi'; my $mech = WWW::Mechanize->new(); $mech->get($url); $sequence='ACAGCNTCTGTTGACTCCCCTGCCAGCAAGCCCACTGGCCTGAGCACATGAAGTCCTGC +ACCCAGTCAGGAGAAGGAGCGCTCCTGGCACCAGCAGGAGCTGGCAAAGGCTCTGGAGAGCTTAGAAAG +GGAAAAAATGGAGCTGGAAATGAGGCTAAAGGAGCAGCAGACAGAAATGGAGGCCATCCAGGCCCAGAG +GGAAGAAGAACGGACCCAGGCAGAGAGTGCCCTATGCCAGATGCAGCTGGAAACAGAGAAGGAGAGAGT +ATCCCTCCTGGAGACACTGCTGCAGACGCAGAAGGAGCTAGCAGATGCCAGCCAACAACTGGACGGAAC +TGAGGCAA'; $primernum='1'; $mech->submit_form( form_number => 1, fields => { SEQUENCE => $sequence, PRIMER_NUM_RETURN => $primernum, } ); # and so fourth...
    This code is untested as I dont have internet access from a machine running perl at the moment.
    The documentation for WWW::Mechanize .
    Let me how that turns out.

    Martin
      Martin,

      Thanks for the reply. However, I get the same response using both sets of code. I think the code you posted and mine are functionally the same, if my reading of the docs is correct, except that submit_form does not click a button for the form submission. Since the CGI is looking for the submit button parameter to be present and true, I think I need the "click" method there.

      Sean
        Hi Sean,

        Sorry that didnt work.
        Try using the following to submit the form rather than the method I suggested:
        $mech->submit();
        From the docs: "Submits the page, without specifying a button to click. Actually, no button is clicked at all."
        I think that using your code you posted and the $mech->submit(); rather than the $mech->click(); method may help.

        Thanks.

        Martin
Re: WWW::Mechanize and submit button question
by saberworks (Curate) on Aug 08, 2005 at 16:02 UTC
    Since button text gets sent as a normal paremeter anyway, why not set it as a field and them submit it, rather than trying to click the button?
      Thanks for the reply, but no go here, either. Just for my edification, why should the two methods be different?

      Sean

        Well, they shouldn't. I figured maybe they were doing something weird with buttons & clicks, but apparently not :( I spent some time on this problem, and I have experienced the same thing you have. It doesn't matter how I use Mech to submit the "Pick Primers" - it always spits out that error. I managed to find an older version of thier code (I can't find the currently running CGI, they have only released old ones). Here is the line:
        if ($query->param('Pick Primers')) { process_input($query); } else { confess "Did not see the 'Pick Primers' query parameter" }
        So it's nothing fishy, I think the encoding is wrong or something. Their form encoding says:

        enctype="x-www-form-urlencoded"

        I wonder if that has anything to do with it? I passed that as an HTTP header and it made no difference. I think the only solution here is to use a proxy to sniff what your browser is sending and then use the same proxy to sniff what Mech is sending and try to reconcile the two.

        Sorry I couldn't have been of more help.