Hi Monks, I can not figure out why the two programs below cause different responses from server.

The server returns a string (presenting an array) and then you can request and fetch deltas from your last request to get the array via a header.

Using LWP::UserAgent things work fine:

use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new(keep_alive => 1); my $url = "http://example.com/myUrl"; my $token = undef; while(1) { my $res = $ua->get($url, 'Start-From' => $token // "", 'X-AcceptDe +ltaUpdates' => 'true'); $token = $res->headers->{'token'} if ($res->headers->{'token'}); print $res->decoded_content if ($res->is_success); sleep 4; }
Array is: ['item1','item2'] Add ['item3'] No update Add ['item4'] No update Add ['item5'] No update ...

However using HTTP::AnyEvent I get bizarre behaviour (this is a silly way to do timing I know but I am just keeping things as simple as possible for testing):

use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; my $url = "http://example.com/myUrl"; my $token = undef; while(1) { my $cv = AnyEvent::condvar; my $instance = http_request GET => $url, headers => { 'Start-From' => $token // "", 'X-AcceptDeltaUpdat +es' => 'true' }, keepalive => 1, persistent => 1, sub { my ($data, $headers) = @_; $token = $headers->{'token'} if (defined $headers->{'token +'}); print $data if ($data); $cv->send; }; $cv->recv; $cv = AnyEvent::condvar; my $timer = AnyEvent->timer(after=>4, interval => 4, cb => sub { $cv->send; }); $cv->recv; }
Array is: ['item1','item2'] No update No update No update No update No update No update Array is: ['item1','item2','item3','item4'] No update No update No update No update No update No update ...

I have tried comparing the headers sent (by setting up a HTTPD server) and all the requests seem to be identical with headers sent. So I don't understand the difference. I am basically about to resort to using wireshark to see if I can determine what is different at a TCP/HTTP level. But is there anything obvious I am missing here?


In reply to Difference between LWP::UserAgent and AnyEvent::HTTP by sectokia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.