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

I have a LWP object in which I have to add aditional content to the post. For example:
my $response = $browser->post($url, [ 'Encounter1847760942' => "$enc_id", 'SelectType1847760942' => "StartOnSelect", 'Begin1847760942' => "01/02/2005", 'DaysBack1847760942' => "3", 'ProcedureSearchText1847760942' => "", 'EncounterOptionSelected' => "false" ], @ns_headers);
There are additional fields that I have to add to the content portion of the post. How do I do this?
$response = $browser->content($poststring); # where $poststring = 'checkbox'=>\"1\"" etc....
This does not seem to work. Am I missing something simple here? Thanks Rob

Replies are listed 'Best First'.
Re: Adding to Content on LWP Post
by blazar (Canon) on Jan 11, 2005 at 11:29 UTC
    $response = $browser->content($poststring); # where $poststring = 'checkbox'=>\"1\"" etc....
    Whatever... are you sure you don't want e.g.

    # $poststring = ['checkbox'=> ...]

    Also, IMHO you'd better use alternate delimiters so to avoid excessive and error-prone escaping.

Re: Adding to Content on LWP Post
by Util (Priest) on Jan 18, 2005 at 18:24 UTC

    Once you call the post() method, your request is transmitted to the web server; it is too late to modify it. You need to store the form data in a structure that you can add to at runtime, and then use that structure in the post() call.

    Try these array-based techniques to initialize and manipulate your form data:

    my @form_data_pairs = ( 'Encounter1847760942' => $enc_id, 'SelectType1847760942' => 'StartOnSelect', 'Begin1847760942' => '01/02/2005', 'DaysBack1847760942' => '3', 'ProcedureSearchText1847760942' => '', 'EncounterOptionSelected' => 'false', ); # ... Code to handle dynamic options would go here ... my $cb = ($some_dynamic_flag) ? 1 : 0; push @form_data_pairs, ( 'checkbox' => $cb, 'something' => 'Something else', ); push @form_data_pairs, ('checkbox2' => 1) if $some_other_flag; my $response = $browser->post($url, \@form_data_pairs, @ns_headers);

    If the form has no duplicate data elements, and the receiving application is not neurotic about parameter order, then hash-based techniques may be clearer and more flexible:

    my %form_data = ( 'Encounter1847760942' => $enc_id, 'SelectType1847760942' => 'StartOnSelect', 'Begin1847760942' => '01/02/2005', 'DaysBack1847760942' => '3', 'ProcedureSearchText1847760942' => '', 'EncounterOptionSelected' => 'false', ); # ... Code to handle dynamic options would go here ... $form_data{'checkbox'} = ($some_dynamic_flag) ? 1 : 0; $form_data{'something'} = 'Something else'; $form_data{'checkbox2'} = 1 if $some_other_flag; my $response = $browser->post($url, [ %form_data ], @ns_headers);