The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::Manual::HTTPGuide - HTTP Guide

OVERVIEW

Mojo contains a full featured HTTP 1.1 client/server implementation.

There are similarities to LWP, but Mojo is much more server oriented. In fact the HTTP client was built specifically for testing web applications.

One of the main design goals was portability, so it features pure Perl implementations of FastCGI, CGI and a single process test server.

CLIENT

The Mojo::Client was specifically built for stress testing web applications and uses asynchronous io operations to do this in a very portable way, without the need to use multiple processes.

A Simple Request

Here is a simple example to get you an idea of how it will look in action.

    use Mojo::Client;
    use Mojo::Transaction;

    # Build a new client
    my $client = Mojo::Client->new;

    # Build a new transaction
    my $tx = Mojo::Transaction->new_post('http://kraih.com');

    # Configure the request
    $tx->req->body->content('foo bar baz' x 1024);

    # Process the transaction in a blocking operation
    $client->process_all($tx);

    # Play with the response
    print $tx->res->headers->content_type;

Parallel Requests

Parallel requests are equally simple.

    use Mojo::Client;
    use Mojo::Transaction;

    # Build a new client
    my $client = Mojo::Client->new;

    # Build new transactions
    my $tx1 = Mojo::Transaction->new_post('http://kraih.com');
    my $tx2 = Mojo::Transaction->new_get'http://labs.kraih.com');

    # Configure the requests
    $tx1->req->headers->expect('100-continue');
    $tx1->req->body->content('foo bar baz' x 1024);

    $tx2->req->body->content('foo bar baz');

    # Process the transactions in a blocking operation
    $client->process_all($tx1, $tx2);

    # Play with the responses
    print $tx1->res->headers->content_type;
    print $tx2->res->headers->content_type;