Francisco Zarabozo Francisco Zarabozo - 5 months ago 28
Perl Question

Fetch text/event-stream web response using WWW::Mechanize

I'm using

to fetch a web page that includes a Google Maps widget that receives constant data from a single response of type text/event-stream.

That kind of response is like a never ending response from the server that constantly returns updated data for the widget to work.

I'm trying to find out how to read that exact response from Perl. Using something like:

my $mech = WWW::Mechanize->new;

# Do some normal GET and POST requests to authenticate and set cookies for the session

# Now try to get that text/event-stream response

$mech->get('https://some.domain.com/event_stream_page');


But that doesn't work because the response never ends.

How can I make that request and start reading the response and do something with that data every time the server updates the stream?

Answer

Found a way to do this. Using a handler from LWP, from which WWW::Mechanize inherits:

$mech->add_handler (
    'response_data',
    sub {
        my ($response, $ua, $h, $data) = @_;
        # Your chunk of response is now in $data
        # Important to return a true value if you want to keep reading the response!
        return 1;
    },
);
Comments