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

Hi,

I want to send the following headers in a POST from a perl script. Could anyone tell me how to do this in perl please?

My aim is to set up a link for users which posts this information for them when they click the link.

Thanks,

js1.

POST /ess/SignUpAsEmployee.aspx HTTP/1.1 Host: ltrbod-apdhz005 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Ge +cko/20051111 Firefox/1.5 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9 +,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://ltrbod-apdhz005/ess/SignUpAsEmployee.aspx Cookie: SSOCookie=Rsm04vXQBeQOi6o8aUarAGNgzKHCGxt2jVYcmOuleLX4dw5cIQVH +BkbxOmXX% ASP.NET_SessionId=lrtixumpwebrpheikmwni5i4; Content-Type: application/x-www-form-urlencoded Content-Length: 309 __VIEWSTATE=dDwxNTk4MDgzOTc1O3Q8O2w8aTwyPjs%2BO2w8dDw7bDxpPDE%2BOz47bD +x0PDtsPGk8Mz47aTw1Pjs%2BO2w8dDxwyLzEyLzIwMDUgMTU6MTc7Pj47Pjs7Pjs%2BPj +s%2BPjs%2BPjs%2BBfgZ5IQIMSNKJj7%2BLszqPx1I1Co%3D&txtEmployeeID=842111 +2&BtnSignUp=Sign+Up HTTP/1.x 200 OK Set-Cookie: SSOCookie=dnUwJ2FnRZtNcxpzA12uuPUAhRTCgjUXaX6iCsFK30bGyUn9 +22FlHbucatneD3HeT26Frz38e2keHTZ9Mh4YC0e9gI8kVJoG%2B%2Bvab58%2FHT5RU; +path=/; Date: Thu, 22 Dec 2005 15:18:22 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 1.1.4322 Cache-Control: private Content-Type: text/html; charset=utf-8 Content-Length: 6679

Replies are listed 'Best First'.
Re: link to post hidden fields
by Corion (Patriarch) on Dec 22, 2005 at 16:17 UTC
Re: link to post hidden fields
by blazar (Canon) on Dec 22, 2005 at 16:11 UTC
    Short answer: check perldoc LWP! Long answer contained therein...
Re: link to post hidden fields
by tomfahle (Priest) on Dec 22, 2005 at 18:08 UTC
    As mentioned before:
    There are the LWP::UserAgent and HTTP::Request modules, which together allow you to make POST requests.
    Example:
    use strict; use HTTP::Request::Common qw(POST); my $url = 'http://www.somewhere.com/cgi-bin/formular.cgi'; my $req = POST $url, [ Host => 'myHost', Server => 'Any Value', OtherField => 'Some Value', OneMoreField => 'whatever', ]; # Just a debug to STDOUT print "HTTP-FullRequest-Header: \n"; print $req->headers->as_string() , "\n"; print "HTTP-FullRequest-Header-Content: \n"; print $req->content() ,"\n"; # Now send it use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $response = $ua->request($req); if ( $response->is_error() ) { print "Error-Code : ", $response->code() , "\n"; print "Error-Message : ", $response->message() , "\n"; } else { print $response->content() , "\n"; }

    HTH

      Thanks for everyone's replies. I tried HTTP::Request::FromTemplate because that looked easiest but for some reason the request wasn't getting sent.

      The last suggestion is semi-working for me in that the request is getting sent and the browser goes to the site, but I'm not actually getting the desired page.

      I just wanted to check if I've set this up right with regards to the viewstate content below?

      my $url = 'http://lhtbod-apdhz001.uk.ad.com/ess/SignUpAsEmployee.aspx' +; my $content='__VIEWSTATE=dDwxNTk4MDgzOTc1O3Q8O2w8aTwyPjs%2BO2w8dDw7bDx +pPDE%2BOz47bDx0PDtsPGk8Mz47aTw1Pjs%2BO2w8dDxwPHA8bDxUZX h0Oz47bDxBZG1pbmlzdHJhdG9yOiA3NTIzNTk7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7Pjts +PDIyLzEyLzIwMDUgMTU6MTc7Pj47Pjs7Pjs%2BPjs%2BPjs%2BPjs%2 BBfgZ5IQIMSNKJj7%2BLszqPx1I1Co%3D&txtEmployeeID=84212&BtnSignUp=Sign+U +p'; my $req = POST $url, [ 'Host' => 'lhtbod-apdhz001.uk.ad.com', 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; e +n-US; rv:1.8) Gecko/20051111 Firefox/1.5', 'Accept' => 'text/xml,application/xml,application/xhtml ++xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0 .5', 'Accept-Language' => 'en-us,en;q=0.5', 'Accept-Encoding' => 'gzip,deflate', 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive' => '300', 'Connection' => 'keep-alive', 'Referer' => 'http://lhrbod-apdhz001/ess/SignUpAsEmploye +e.aspx', 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => '309', 'Content' => $content, ];
Re: link to post hidden fields
by planetscape (Chancellor) on Dec 23, 2005 at 02:09 UTC

    I'm not 100% sure I understand what you are trying to accomplish, but... My boilerplate advice is this:

    1. Use WWW::Mechanize.

    2. I would try using a module such as HTTP::Recorder or WWW::Mechanize::Shell to record a successful manual form submission. The output of HTTP::Recorder, for instance, can be "dropped" right into your WWW::Mechanize scripts.

    3. Another important tool for finding out what is really happening behind the scenes between server and browser is a protocol analyzer such as Ethereal.

    4. Don't forget to view the source of the page you're trying to automate. A number of times, the solution has been as simple as making sure one is not trying to stick 10 characters in a field made for 8, or that one is referring to a control by its name, not its value.

    Good luck,

    planetscape
      Hi there.
      There are issues with mechanize and ASP::Net.
      You need to pass in the following hidden fields :
      __EVENTTARGET
      __EVENTARGUMENT
      __VIEWSTATE,
      __LASTFOCUS
      see also cpan HTML::TreeBuilderX::ASP_NET.
      
      hope that helps.
      mike
      
      bless(\sub {bless \$this,&$that;},"Cool");