[ic] HTTP POST, just about there

Marc Brevoort interchange-users@icdevgroup.org
Fri Feb 14 11:43:00 2003


Okay, here is what I have so far:

1. A tag called posttourl, stored in file 
/usr/lib/interchange/lib/UI/usertag/posttourl.tag 
with the following contents:

#---- 8< snip snip 8<-----
Usertag posttourl Order url params
Usertag posttourl Routine <<EOR
sub {
require HTTP::Request;
require HTTP::Headers;
require LWP::UserAgent;

        my $url=shift;
        my $params=shift;
        my $A="";
#       $A="url=$url<br>params=$params<br>";
        my $ua = LWP::UserAgent->new;
        my $request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content($params);
	$request->content_length(length($params));

        my $response = $ua->request($request);
        if ($response->is_success) {
         $A .=  $response->content;
        } else {
         $A .= $response->error_as_HTML;
      }
        return $A;
}
EOR
#---- 8< snip snip 8<-----

2. A script using it, as follows:
#---- 8< snip snip 8<-----
<HTML>
<BODY>
[perl]
        $url="http://this.is.some.url";
        $params="submitbutton=Add+Existing+Database";
        my $to_eval="[posttourl $url $params]";
        return $to_eval;
[/perl]
</BODY>
</HTML>
#---- 8< snip snip 8<-----

It turns out that Interchange interpolates the tag resulting from the
perl block, which effectively allows for dynamic parameters.

Error handling for errors such as 302 Moved do not work yet (of course),
but in my case this isn't necessarily a problem.
-- 
Marc Brevoort

e-mail:	marc.brevoort@armazemdedados.com
web:	http://www.armazemdedados.com

Armazem de Dados, Informatica, Lda
Dep. Desenvolvimento
Tel. +351 21 910 83 10 / Fax. +351- 21 910 83 19