1 min Different ways of making a POST request with Perl #perl #programming use REST::Client; use LWP; use WWW::Mechanize; sub test_create_user_account { my $username = shift; my $email = shift; my $function = "createnewuser"; my $domain = Config::get_value_for("email_host"); my $prog = Config::get_value_for("cgi_app"); # USING LWP # my $url = "http://$domain" . "$prog/$function"; # my $browser = LWP::UserAgent->new; # my $response = $browser->post( $url, # [ # 'username' => $username, # 'email' => $email # ], # ); # return $response->content; # USING WWW::Mechanize my $mech = WWW::Mechanize->new(); my $url = "http://jothut.com/cgi-bin/app.pl/signup"; $mech->get($url); $mech->submit_form ( form_number => 2, fields => { username => $username, email => $email } ); return $mech->content(); # USING REST # my $headers = { # 'Content-type' => 'application/x-www-form-urlencoded' # }; # set up a REST session # my $rest = REST::Client->new( { # host => "http://$domain" . "$prog", # } ); # then we have to url encode the params that we want in the body # my $pdata = { # 'username' => $username, # 'email' => $email # }; # my $params = $rest->buildQuery( $pdata ); # but buildQuery() prepends a '?' so we strip that out # $params =~ s/\?//; # then sent the request: # POST requests have 3 args: URL, BODY, HEADERS # $rest->POST( "/$function" , $params , $headers ); # return $rest->responseContent(); } From JR's : articles 212 words - 1607 chars - 1 min read created on Oct 15, 2013 at 12:33:41 pm updated on Oct 15, 2013 at 12:34:42 pm - # source - versions