how can perl respond to someone's HTTP POST

J

Jack

Hi there,

Simple concept but its not clear anywhere how to implement a response
to a server (that has posted to my server) with some result. does
anyone know how, is a CPAN module required to do this ? I have seen
LWP for posting, but its not clear how to RESPOND to somebody's post
to my server...

thank you,
Jack
 
L

Lars Eighner

In our last episode,
the said:
Hi there,
Simple concept but its not clear anywhere how to implement a response
to a server (that has posted to my server) with some result. does
anyone know how, is a CPAN module required to do this ? I have seen
LWP for posting, but its not clear how to RESPOND to somebody's post
to my server...

Your post does not clearly indicate to me that you know the difference
between the several sense of the word "post." So I will say something about
the part that most nearly has something to do with perl.

In this situation:

You have set up a web page which contains an HTML form using the POST
method.

Perhaps you are asking:

In perl how do I do what should be done with the POST data and get the
server to send a page with some response to the data that has been recieved
via POST?

In that case the answer is something like this:

look for your answer in man CGI (unix-like systems) or perldoc CGI (any
system with perl installed). The CGI module has been part of the core
distribution for some time. You may find some additional help in section 9
of the FAQ "Networking." You can use the CGI module without an especially
deep understanding of what is going on behind the scenes.

Eventually you should understand that the perl CGI module is an easy way to
do CGI with perl, but neither perl nor CGI.pm (the module) is CGI. CGI is
the standard for getting servers and executables to interact. Just about
any executable that can write to stdout can be coerced into complying with
CGI, and that includes perl without the CGI module.

If the man page (or perldoc) is not enough help, you should be able to
google up a number of tutorials with keywords like perl, CGI, tutorial.

Whether any kind of CGI (perl, C, sh, php-cgi, etc.) will work for you at
all depends upon your server and configuration. So if you try to work
through a tutorial and cannot get your test documents to work at all, you
need to look at your server and its requirements.
 
M

Mart van de Wege

Jack said:
Hi there,

Simple concept but its not clear anywhere how to implement a response
to a server (that has posted to my server) with some result. does
anyone know how, is a CPAN module required to do this ? I have seen
LWP for posting, but its not clear how to RESPOND to somebody's post
to my server...
Well, if you know how the other end handles your client LWP POSTs, then
you should have a good clue...

In short, you have to write a HTTP server, or hook into an existing
one. Perl has the tools to do both, but before I go into details, I
really need to know what you do have on your end. Do you have a HTTP
server already running? If yes, which one?

Mart
 
J

Jack

Well, if you know how the other end handles your client LWP POSTs, then
you should have a good clue...

In short, you have to write a HTTP server, or hook into an existing
one. Perl has the tools to do both, but before I go into details, I
really need to know what you do have on your end. Do you have a HTTP
server already running? If yes, which one?

Mart

Hi I am using a Windows Server 2003 system running IIS. I have other
computers on the web that will be posting to a URL of mine (a perl ASP
webpage), and that webpage acquires those posted variables, does a
database lookup, and all I want to do is return the lookup results to
the other system that requested it via a post.. ! Basic. is this
possible, or do I just simply post back to the other server. I am
familiar with using LWP for posting to a URL, but again, I have no
idea, and would like to see the code, to provide a direct REPLY with
data back to the poster. I am not looking for a digression on "man
CGI", as the first poster indicated.
Thank you !

Jack
 
J

Jim Gibson

Jack said:
Hi there,

Simple concept but its not clear anywhere how to implement a response
to a server (that has posted to my server) with some result. does
anyone know how, is a CPAN module required to do this ? I have seen
LWP for posting, but its not clear how to RESPOND to somebody's post
to my server...

A server that is posting a request to your server is acting as a
client. The way to generate a dynamic response to that request is to
have the URL of the request point to a program (or programmable page)
instead of a static page. The CGI protocol is one way to do this. There
are others: ASP, JSP, PHP, etc.

LWP is a client system that may be used to emulate a browser. It does
not respond to requests.
 
J

Jack

A server that is posting a request to your server is acting as a
client. The way to generate a dynamic response to that request is to
have the URL of the request point to a program (or programmable page)
instead of a static page. The CGI protocol is one way to do this. There
are others: ASP, JSP, PHP, etc.

LWP is a client system that may be used to emulate a browser. It does
not respond to requests.

Exactly..BUT, how exactly do I have the Perl/ASP page respond back ?
"print" to the page ? Trying to get tactical level here, ie code.
 
M

Mart van de Wege

Jack said:
Hi I am using a Windows Server 2003 system running IIS. I have other
computers on the web that will be posting to a URL of mine (a perl ASP
webpage), and that webpage acquires those posted variables, does a
database lookup, and all I want to do is return the lookup results to
the other system that requested it via a post.. ! Basic. is this
possible, or do I just simply post back to the other server. I am
familiar with using LWP for posting to a URL, but again, I have no
idea, and would like to see the code, to provide a direct REPLY with
data back to the poster. I am not looking for a digression on "man
CGI", as the first poster indicated.
Thank you !


First off, snip signatures, please.

Second, a digression on CGI is *exactly* what you need.

On the gripping hand, I don't know anything about Windows and IIS, so
how you would go about serverside programming in that environment I am
going to have to leave to someone else.

Mart
 
J

Jack

At 2009-05-29 11:08AM, "Jack" wrote:
[...]
 Hi I am using a Windows Server 2003 system running IIS.  I have other
 computers on the web that will be posting to a URL of mine (a perl ASP
 webpage), and that webpage acquires those posted variables, does a
 database lookup, and all I want to do is return the lookup results to
 the other system that requested it via a post.. !  Basic.  is this
 possible, or do I just simply post back to the other server.  I am
 familiar with using LWP for posting to a URL, but again, I have no
 idea, and would like to see the code, to provide a direct REPLY with
 data back to the poster.  I am not looking for a digression on "man
 CGI", as the first poster indicated.

If you had given us these details in the first place, you might have
been spared that horrifying "digression"

Let your web server do it's job.  Your asp page doesn't have to worry
about how the request got to it -- it just needs to return some content.

What format for the data are you supposed to provide?  Will CSV do?

IMO, your asp should output a Content-Type: text/plain header followed
by properly-quoted CSV data.

Thanks ! actually it will output XML, but just knowing how to output
anything is a start - what is the code I need to provide so this
happens ?? My ASP page does a database lookup, then what is the code
I need to hand off the contents of these variables to something ??
 
S

Steve C

Jack said:
Exactly..BUT, how exactly do I have the Perl/ASP page respond back ?
"print" to the page ? Trying to get tactical level here, ie code.


The way the CGI protocol works is that the web server (IIS) receives the
request and calls the perl program on the server, which outputs HTML to
stdout. That output is captured by the web server and returned to the
requester.

The perl CGI module does some of the work for you, like hiding whether the
request is GET or POST. There are many tutorials about CGI available,
here is one:
http://support.microsoft.com/kb/245225
 
J

John

Jack said:
Hi there,

Simple concept but its not clear anywhere how to implement a response
to a server (that has posted to my server) with some result. does
anyone know how, is a CPAN module required to do this ? I have seen
LWP for posting, but its not clear how to RESPOND to somebody's post
to my server...

thank you,
Jack

Hi. I've cut and pasted from my own code.
I guess this is what you need.
Regards
John


#!/usr/bin/perl
# Perl 5.8.8
# MySQL 5.0.32

use lib '../'; # parent directory
use strict;
use warnings;
use XML::Simple; $XML::Simple::pREFERRED_PARSER='XML::parser';
my $xmlsimple=new XML::Simple (ForceArray=>1, SuppressEmpty=>'',
KeyAttr=>[]); # create object

# ------------------------------------------------------------------------------------------

my $length=$ENV{'CONTENT_LENGTH'};
my $request; my $response;
read (STDIN,$request,$length); # XML request from client
my $data=$xmlsimple->XMLin($request); # parse XML string

my $username=$data->{'username'}->[0];
my $password=$data->{'password'}->[0];
my $number=$data->{'number'}->[0];

if ($username ne 'whatever') {$response="
<whatever-response>
<error-message>invalid username</error-message>
</whatever-response>"
}
else {$response="
<whatever-response>
<company-name>Whatever Limited</company-name>
<company-address>Whatever Town</company-address>
<error-message></error-message>
</whatever-response>"
}

my $len=length($response);

# send 200 back to client via STDOUT - note blank line

print <<END;
Status: 200 OK
Content-type: application/xml;charset=UTF-8
Accept: application/xml;charset=UTF-8
Content-length: $len

$response
END

# ------------------------------------------------------------------------------------------

1; # last statement executed must be true

__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top