Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Communicating to AJAX

by zer (Deacon)
on Dec 28, 2007 at 10:34 UTC ( #659320=perlquestion: print w/replies, xml ) Need Help??

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

Good evening,

I am attempting to communicate directly to a server to test ajax responses. I have attempted using mozilla::mechanize and selenium however due to errors they dont run.

I need to mimic the following post with variations to the url. I am using WWW::Mechanize as it handles the cookies and other interactions that i am doing very well.

Http Header Capture

http://[ADDRESS]/fbjs_ajax_proxy.php POST /fbjs_ajax_proxy.php HTTP/1.1 Host: [ADDRESS] User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko +/20071204 Ubuntu/7.10 (gutsy) Firefox/ 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 Content-Type: application/x-www-form-urlencoded Referer: http://[ADDRESS]/Profile.php?uid=[IDNUM] Content-Length: 889 Cookie: c_user=227302802; sid=2; __utma=87286159.462992557.1198733524. +1198820048.1198836479.9; xs=99ba8adfbde7929b813ade803a0b797f; G6X9=2- +2-061F577DD020161F5618F04BC46C536DA6F6D020489A14211B78600B73C43527-F3 +A883B35D6CD6D9F582182CF2FB69666221D9F5D2BE2BD79EAA2E5A389C58A5; login +_x=227302802; __utmz=87286159.1198836479.9.8.utmccn=(referral)|utmcsr|utmcct=/profile.php|utmcmd=referral; made_write +_conn=1198836470; isfbe=false; __utmb=87286159; __utmc=87286159 Pragma: no-cache Cache-Control: no-cache url=http%3A%2F%2F[IP ADDRESS]%2FpostComments.php%3Fuid%3D227302802%26h +erd%3D&query%5Bcomment%5D=text%20messages%20are%20cool&query%5Bcomman +d%5D=&post_form_id=134f8cf869f0852535eb9c362517960b&type=2&require_lo +gin=true&fb_mockajax_context=O%3A16%3A%22CanvasFBMLFlavor%22%3A1%3A%7 +Bs%3A9%3A%22_fbml_env%22%3Ba%3A8%3A%7Bs%3A4%3A%22user%22%3Bi%3A227302 +802%3Bs%3A6%3A%22app_id%22%3Bs%3A10%3A%222437228683%22%3Bs%3A10%3A%22 +fb_page_id%22%3Bi%3A0%3Bs%3A14%3A%22unfiltered_css%22%3Bb%3A0%3Bs%3A1 +0%3A%22canvas_url%22%3Bs%3A66%3A%22http%3A%2F%2F[reffered url]%2FProf +ile.php%3Fuid%3D[IDNUM]%22%3Bs%3A10%3A%22source_url%22%3Bs%3A70%3A%22 +http%3A%2F%2F[IP ADDRESS]%2FProfile.php%3Fuid%3D[IDNUM]%22%3Bs%3A9%3A +%22loggedout%22%3Bb%3A0%3Bs%3A7%3A%22non-tos%22%3Bb%3A0%3B%7D%7D&fb_m +ockajax_context_hash=f1247ab4b99b&appid=2437228683

I was looking at LWP::UserAgent as it is a part of the WWW::Mechanize->request(). Is this possible? The documentation doesn't touch on this in the ways id like it to. Any guidance would be awsome, especially examples (like with the header i provided)

Thank you for reading and to those who help!

Replies are listed 'Best First'.
Re: Communicating to AJAX
by polettix (Vicar) on Dec 28, 2007 at 10:56 UTC
    You're interested in the contents, but you're looking at the carrier - what you need is HTTP::Request, probably:
    #!/usr/bin/perl use strict; use warnings; use HTTP::Request; my $host = ''; my $uri = "http://$host/path/to/resource"; my $request = HTTP::Request->new(POST => $uri, [ Host => $host, 'User-Agent' => 'Mozilla whatever...', Accept => 'text/xml,application/xml,...', 'Accept-Language' => 'en-us,en;q=0.5', # you get the idea... ]); print {*STDOUT} $request->as_string(); __END__ poletti@PolettiX:~/sviluppo/perl$ perl POST Accept: text/xml,application/xml,... Accept-Language: en-us,en;q=0.5 Host: User-Agent: Mozilla whatever...
    And yes, if you have a WWW::Mechanize (say $mech) object, you can then simply call:
    my $response = $mech->request($request);
    You have probably to work out if cookies are automatically inserted or not, and act consequently.

    Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Io ho capito... ma tu che hai detto?
Re: Communicating to AJAX
by lihao (Monk) on Dec 28, 2007 at 19:27 UTC

    If you just want to check/debug the AJAX response, you can try Firebug (a Firefox extension) which is very handy for this.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://659320]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-08-08 20:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found