HTTP Guide

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.

Mojo 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;

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

# Build a new transaction (Mojo::Transcation)
my $tx = $client->new_tx;

# Configure the request
$tx->req->method('POST');
$tx->req->url->parse('http://kraih.com');
$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;

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

# Build new transactions
my $tx1 = $client->new_tx;
my $tx2 = $client->new_tx;

# Configure the requests
$tx1->req->method('POST');
$tx1->req->url->parse('http://kraih.com');
$tx1->req->headers->expect('100-continue');
$tx1->req->body->content('foo bar baz' x 1024);

$tx2->req->method('GET');
$tx2->req->url->parse('http://labs.kraih.com');
$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;