how can perl respond to someone's HTTP POST

Discussion in 'Perl Misc' started by Jack, May 29, 2009.

  1. Jack

    Jack Guest

    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
    Jack, May 29, 2009
    #1
    1. Advertising

  2. Jack

    Lars Eighner Guest

    In our last episode,
    <>, the
    lovely and talented Jack broadcast on comp.lang.perl.misc:

    > 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.


    --
    Lars Eighner <http://larseighner.com/>
    128 days since Rick Warren prayed over Bush's third term.
    Obama: No hope, no change, more of the same. Yes, he can, but no, he won't.
    Lars Eighner, May 29, 2009
    #2
    1. Advertising

  3. Jack <> writes:

    > 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

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
    Mart van de Wege, May 29, 2009
    #3
  4. Jack

    Jack Guest

    On May 29, 12:21 am, Mart van de Wege <> wrote:
    > Jack <> writes:
    > > 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
    >
    > --
    > "We will need a longer wall when the revolution comes."
    > --- AJS, quoting an uncertain source.


    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
    Jack, May 29, 2009
    #4
  5. Jack

    Jim Gibson Guest

    In article
    <>,
    Jack <> wrote:

    > 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.

    --
    Jim Gibson
    Jim Gibson, May 29, 2009
    #5
  6. Jack

    Jack Guest

    On May 29, 8:29 am, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    > Jack <> wrote:
    > > 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.
    >
    > --
    > Jim Gibson


    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.
    Jack, May 29, 2009
    #6
  7. Jack <> writes:

    > On May 29, 12:21 am, Mart van de Wege <> wrote:
    >> Jack <> writes:
    >> > 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
    >>

    >
    > 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

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
    Mart van de Wege, May 29, 2009
    #7
  8. Jack

    Jack Guest

    On May 29, 8:55 am, Glenn Jackman <> wrote:
    > 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.
    >
    > --
    > Glenn Jackman
    >     Write a wise saying and your name will live forever. -- Anonymous


    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 ??
    Jack, May 29, 2009
    #8
  9. Jack

    Steve C Guest

    Jack wrote:
    > On May 29, 8:29 am, Jim Gibson <> wrote:
    >> In article
    >> <>,
    >>
    >> Jack <> wrote:
    >>> 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.
    >>
    >> --
    >> Jim Gibson

    >
    > 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
    Steve C, May 29, 2009
    #9
  10. Jack

    John Guest

    "Jack" <> wrote in message
    news:...
    > 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__
    John, May 30, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sunny via .NET 247
    Replies:
    0
    Views:
    2,475
    Sunny via .NET 247
    Apr 27, 2004
  2. starshipRobot

    Respond to HTTP Post

    starshipRobot, Jan 13, 2010, in forum: .NET
    Replies:
    0
    Views:
    592
    starshipRobot
    Jan 13, 2010
  3. Francois
    Replies:
    1
    Views:
    122
    Dan Rogers
    Nov 23, 2004
  4. Fred
    Replies:
    17
    Views:
    248
  5. Mike
    Replies:
    1
    Views:
    144
    kaeli
    Sep 8, 2004
Loading...

Share This Page