.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::UserAgent 3pm" .TH Mojo::UserAgent 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::UserAgent \- Non\-blocking I/O HTTP and WebSocket user agent .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::UserAgent; \& \& # Fine grained response handling (dies on connection errors) \& my $ua = Mojo::UserAgent\->new; \& my $res = $ua\->get(\*(Aqdocs.mojolicious.org\*(Aq)\->result; \& if ($res\->is_success) { say $res\->body } \& elsif ($res\->is_error) { say $res\->message } \& elsif ($res\->code == 301) { say $res\->headers\->location } \& else { say \*(AqWhatever...\*(Aq } \& \& # Say hello to the Unicode snowman and include an Accept header \& say $ua\->get(\*(Aqwww.☃.net?hello=there\*(Aq => {Accept => \*(Aq*/*\*(Aq})\->result\->body; \& \& # Extract data from HTML and XML resources with CSS selectors \& say $ua\->get(\*(Aqwww.perl.org\*(Aq)\->result\->dom\->at(\*(Aqtitle\*(Aq)\->text; \& \& # Scrape the latest headlines from a news site \& say $ua\->get(\*(Aqblogs.perl.org\*(Aq)\->result\->dom\->find(\*(Aqh2 > a\*(Aq)\->map(\*(Aqtext\*(Aq)\->join("\en"); \& \& # IPv6 PUT request with Content\-Type header and content \& my $tx = $ua\->put(\*(Aq[::1]:3000\*(Aq => {\*(AqContent\-Type\*(Aq => \*(Aqtext/plain\*(Aq} => \*(AqHi!\*(Aq); \& \& # Quick JSON API request with Basic authentication \& my $url = Mojo::URL\->new(\*(Aqhttps://example.com/test.json\*(Aq)\->userinfo(\*(Aqsri:☃\*(Aq); \& my $value = $ua\->get($url)\->result\->json; \& \& # JSON POST (application/json) with TLS certificate authentication \& my $tx = $ua\->cert(\*(Aqtls.crt\*(Aq)\->key(\*(Aqtls.key\*(Aq)\->post(\*(Aqhttps://example.com\*(Aq => json => {top => \*(Aqsecret\*(Aq}); \& \& # Form POST (application/x\-www\-form\-urlencoded) \& my $tx = $ua\->post(\*(Aqhttps://metacpan.org/search\*(Aq => form => {q => \*(Aqmojo\*(Aq}); \& \& # Search DuckDuckGo anonymously through Tor \& $ua\->proxy\->http(\*(Aqsocks://127.0.0.1:9050\*(Aq); \& say $ua\->get(\*(Aqapi.3g2upl4pq6kufc4m.onion/?q=mojolicious&format=json\*(Aq)\->result\->json(\*(Aq/Abstract\*(Aq); \& \& # GET request via UNIX domain socket "/tmp/myapp.sock" (percent encoded slash) \& say $ua\->get(\*(Aqhttp+unix://%2Ftmp%2Fmyapp.sock/test\*(Aq)\->result\->body; \& \& # Follow redirects to download Mojolicious from GitHub \& $ua\->max_redirects(5) \& \->get(\*(Aqhttps://www.github.com/mojolicious/mojo/tarball/main\*(Aq) \& \->result\->save_to(\*(Aq/home/sri/mojo.tar.gz\*(Aq); \& \& # Non\-blocking request \& $ua\->get(\*(Aqmojolicious.org\*(Aq => sub ($ua, $tx) { say $tx\->result\->dom\->at(\*(Aqtitle\*(Aq)\->text }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; \& \& # Concurrent non\-blocking requests (synchronized with promises) \& my $mojo_promise = $ua\->get_p(\*(Aqmojolicious.org\*(Aq); \& my $cpan_promise = $ua\->get_p(\*(Aqcpan.org\*(Aq); \& Mojo::Promise\->all($mojo_promise, $cpan_promise)\->then(sub ($mojo, $cpan) { \& say $mojo\->[0]\->result\->dom\->at(\*(Aqtitle\*(Aq)\->text; \& say $cpan\->[0]\->result\->dom\->at(\*(Aqtitle\*(Aq)\->text; \& })\->wait; \& \& # WebSocket connection sending and receiving JSON via UNIX domain socket \& $ua\->websocket(\*(Aqws+unix://%2Ftmp%2Fmyapp.sock/echo.json\*(Aq => sub ($ua, $tx) { \& say \*(AqWebSocket handshake failed!\*(Aq and return unless $tx\->is_websocket; \& $tx\->on(json => sub ($tx, $hash) { \& say "WebSocket message via JSON: $hash\->{msg}"; \& $tx\->finish; \& }); \& $tx\->send({json => {msg => \*(AqHello World!\*(Aq}}); \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::UserAgent is a full featured non-blocking I/O HTTP and WebSocket user agent, with IPv6, TLS, SNI, IDNA, HTTP/SOCKS5 proxy, UNIX domain socket, Comet (long polling), Promises/A+, keep-alive, connection pooling, timeout, cookie, multipart, gzip compression and multiple event loop support. .PP All connections will be reset automatically if a new process has been forked, this allows multiple processes to share the same Mojo::UserAgent object safely. .PP For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the optional modules EV (4.32+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and IO::Socket::SSL (2.009+) will be used automatically if possible. Individual features can also be disabled with the \f(CW\*(C`MOJO_NO_NNR\*(C'\fR, \&\f(CW\*(C`MOJO_NO_SOCKS\*(C'\fR and \f(CW\*(C`MOJO_NO_TLS\*(C'\fR environment variables. .PP See "USER AGENT" in Mojolicious::Guides::Cookbook for more. .SH EVENTS .IX Header "EVENTS" Mojo::UserAgent inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS prepare .IX Subsection "prepare" .Vb 1 \& $ua\->on(prepare => sub ($ua, $tx) {...}); .Ve .PP Emitted whenever a new transaction is being prepared, before relative URLs are rewritten and cookies added. This includes automatically prepared proxy \f(CW\*(C`CONNECT\*(C'\fR requests and followed redirects. .PP .Vb 3 \& $ua\->on(prepare => sub ($ua, $tx) { \& $tx\->req\->url(Mojo::URL\->new(\*(Aq/mock\-mojolicious\*(Aq)) if $tx\->req\->url\->host eq \*(Aqmojolicious.org\*(Aq; \& }); .Ve .SS start .IX Subsection "start" .Vb 1 \& $ua\->on(start => sub ($ua, $tx) {...}); .Ve .PP Emitted whenever a new transaction is about to start. This includes automatically prepared proxy \f(CW\*(C`CONNECT\*(C'\fR requests and followed redirects. .PP .Vb 3 \& $ua\->on(start => sub ($ua, $tx) { \& $tx\->req\->headers\->header(\*(AqX\-Bender\*(Aq => \*(AqBite my shiny metal ass!\*(Aq); \& }); .Ve .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojo::UserAgent implements the following attributes. .SS ca .IX Subsection "ca" .Vb 2 \& my $ca = $ua\->ca; \& $ua = $ua\->ca(\*(Aq/etc/tls/ca.crt\*(Aq); .Ve .PP Path to TLS certificate authority file used to verify the peer certificate, defaults to the value of the \&\f(CW\*(C`MOJO_CA_FILE\*(C'\fR environment variable. .PP .Vb 2 \& # Show certificate authorities for debugging \& IO::Socket::SSL::set_defaults(SSL_verify_callback => sub { say "Authority: $_[2]" and return $_[0] }); .Ve .SS cert .IX Subsection "cert" .Vb 2 \& my $cert = $ua\->cert; \& $ua = $ua\->cert(\*(Aq/etc/tls/client.crt\*(Aq); .Ve .PP Path to TLS certificate file, defaults to the value of the \f(CW\*(C`MOJO_CERT_FILE\*(C'\fR environment variable. .SS connect_timeout .IX Subsection "connect_timeout" .Vb 2 \& my $timeout = $ua\->connect_timeout; \& $ua = $ua\->connect_timeout(5); .Ve .PP Maximum amount of time in seconds establishing a connection may take before getting canceled, defaults to the value of the \f(CW\*(C`MOJO_CONNECT_TIMEOUT\*(C'\fR environment variable or \f(CW10\fR. .SS cookie_jar .IX Subsection "cookie_jar" .Vb 2 \& my $cookie_jar = $ua\->cookie_jar; \& $ua = $ua\->cookie_jar(Mojo::UserAgent::CookieJar\->new); .Ve .PP Cookie jar to use for requests performed by this user agent, defaults to a Mojo::UserAgent::CookieJar object. .PP .Vb 2 \& # Ignore all cookies \& $ua\->cookie_jar\->ignore(sub { 1 }); \& \& # Ignore cookies for public suffixes \& my $ps = IO::Socket::SSL::PublicSuffix\->default; \& $ua\->cookie_jar\->ignore(sub ($cookie) { \& return undef unless my $domain = $cookie\->domain; \& return ($ps\->public_suffix($domain))[0] eq \*(Aq\*(Aq; \& }); \& \& # Add custom cookie to the jar \& $ua\->cookie_jar\->add( \& Mojo::Cookie::Response\->new( \& name => \*(Aqfoo\*(Aq, \& value => \*(Aqbar\*(Aq, \& domain => \*(Aqdocs.mojolicious.org\*(Aq, \& path => \*(Aq/Mojolicious\*(Aq \& ) \& ); .Ve .SS inactivity_timeout .IX Subsection "inactivity_timeout" .Vb 2 \& my $timeout = $ua\->inactivity_timeout; \& $ua = $ua\->inactivity_timeout(15); .Ve .PP Maximum amount of time in seconds a connection can be inactive before getting closed, defaults to the value of the \&\f(CW\*(C`MOJO_INACTIVITY_TIMEOUT\*(C'\fR environment variable or \f(CW40\fR. Setting the value to \f(CW0\fR will allow connections to be inactive indefinitely. .SS insecure .IX Subsection "insecure" .Vb 2 \& my $bool = $ua\->insecure; \& $ua = $ua\->insecure($bool); .Ve .PP Do not require a valid TLS certificate to access HTTPS/WSS sites, defaults to the value of the \f(CW\*(C`MOJO_INSECURE\*(C'\fR environment variable. .PP .Vb 2 \& # Disable TLS certificate verification for testing \& say $ua\->insecure(1)\->get(\*(Aqhttps://127.0.0.1:3000\*(Aq)\->result\->code; .Ve .SS ioloop .IX Subsection "ioloop" .Vb 2 \& my $loop = $ua\->ioloop; \& $ua = $ua\->ioloop(Mojo::IOLoop\->new); .Ve .PP Event loop object to use for blocking I/O operations, defaults to a Mojo::IOLoop object. .SS key .IX Subsection "key" .Vb 2 \& my $key = $ua\->key; \& $ua = $ua\->key(\*(Aq/etc/tls/client.crt\*(Aq); .Ve .PP Path to TLS key file, defaults to the value of the \f(CW\*(C`MOJO_KEY_FILE\*(C'\fR environment variable. .SS max_connections .IX Subsection "max_connections" .Vb 2 \& my $max = $ua\->max_connections; \& $ua = $ua\->max_connections(5); .Ve .PP Maximum number of keep-alive connections that the user agent will retain before it starts closing the oldest ones, defaults to \f(CW5\fR. Setting the value to \f(CW0\fR will prevent any connections from being kept alive. .SS max_redirects .IX Subsection "max_redirects" .Vb 2 \& my $max = $ua\->max_redirects; \& $ua = $ua\->max_redirects(3); .Ve .PP Maximum number of redirects the user agent will follow before it fails, defaults to the value of the \&\f(CW\*(C`MOJO_MAX_REDIRECTS\*(C'\fR environment variable or \f(CW0\fR. .SS max_response_size .IX Subsection "max_response_size" .Vb 2 \& my $max = $ua\->max_response_size; \& $ua = $ua\->max_response_size(16777216); .Ve .PP Maximum response size in bytes, defaults to the value of "max_message_size" in Mojo::Message::Response. Setting the value to \f(CW0\fR will allow responses of indefinite size. Note that increasing this value can also drastically increase memory usage, should you for example attempt to parse an excessively large response body with the methods "dom" in Mojo::Message or "json" in Mojo::Message. .SS proxy .IX Subsection "proxy" .Vb 2 \& my $proxy = $ua\->proxy; \& $ua = $ua\->proxy(Mojo::UserAgent::Proxy\->new); .Ve .PP Proxy manager, defaults to a Mojo::UserAgent::Proxy object. .PP .Vb 2 \& # Detect proxy servers from environment \& $ua\->proxy\->detect; \& \& # Manually configure HTTP proxy (using CONNECT for HTTPS/WebSockets) \& $ua\->proxy\->http(\*(Aqhttp://127.0.0.1:8080\*(Aq)\->https(\*(Aqhttp://127.0.0.1:8080\*(Aq); \& \& # Manually configure Tor (SOCKS5) \& $ua\->proxy\->http(\*(Aqsocks://127.0.0.1:9050\*(Aq)\->https(\*(Aqsocks://127.0.0.1:9050\*(Aq); \& \& # Manually configure UNIX domain socket (using CONNECT for HTTPS/WebSockets) \& $ua\->proxy\->http(\*(Aqhttp+unix://%2Ftmp%2Fproxy.sock\*(Aq) \->https(\*(Aqhttp+unix://%2Ftmp%2Fproxy.sock\*(Aq); .Ve .SS request_timeout .IX Subsection "request_timeout" .Vb 2 \& my $timeout = $ua\->request_timeout; \& $ua = $ua\->request_timeout(5); .Ve .PP Maximum amount of time in seconds establishing a connection, sending the request and receiving a whole response may take before getting canceled, defaults to the value of the \f(CW\*(C`MOJO_REQUEST_TIMEOUT\*(C'\fR environment variable or \f(CW0\fR. Setting the value to \f(CW0\fR will allow the user agent to wait indefinitely. The timeout will reset for every followed redirect. .PP .Vb 2 \& # Total limit of 5 seconds, of which 3 seconds may be spent connecting \& $ua\->max_redirects(0)\->connect_timeout(3)\->request_timeout(5); .Ve .SS server .IX Subsection "server" .Vb 2 \& my $server = $ua\->server; \& $ua = $ua\->server(Mojo::UserAgent::Server\->new); .Ve .PP Application server relative URLs will be processed with, defaults to a Mojo::UserAgent::Server object. .PP .Vb 6 \& # Mock web service \& $ua\->server\->app(Mojolicious\->new); \& $ua\->server\->app\->routes\->get(\*(Aq/time\*(Aq => sub ($c) { \& $c\->render(json => {now => time}); \& }); \& my $time = $ua\->get(\*(Aq/time\*(Aq)\->result\->json\->{now}; \& \& # Change log level \& $ua\->server\->app\->log\->level(\*(Aqfatal\*(Aq); \& \& # Port currently used for processing relative URLs blocking \& say $ua\->server\->url\->port; \& \& # Port currently used for processing relative URLs non\-blocking \& say $ua\->server\->nb_url\->port; .Ve .SS socket_options .IX Subsection "socket_options" .Vb 2 \& my $options = $ua\->socket_options; \& $ua = $ua\->socket_options({LocalAddr => \*(Aq127.0.0.1\*(Aq}); .Ve .PP Additional options for IO::Socket::IP when opening new connections. .SS tls_options .IX Subsection "tls_options" .Vb 2 \& my $options = $ua\->tls_options; \& $ua = $ua\->tls_options({SSL_cipher_list => \*(AqDEFAULT:!DH@SECLEVEL=1\*(Aq}); .Ve .PP Additional options for IO::Socket::SSL when opening new connections. .SS transactor .IX Subsection "transactor" .Vb 2 \& my $t = $ua\->transactor; \& $ua = $ua\->transactor(Mojo::UserAgent::Transactor\->new); .Ve .PP Transaction builder, defaults to a Mojo::UserAgent::Transactor object. .PP .Vb 2 \& # Change name of user agent \& $ua\->transactor\->name(\*(AqMyUA 1.0\*(Aq); \& \& # Disable compression \& $ua\->transactor\->compressed(0); .Ve .SH METHODS .IX Header "METHODS" Mojo::UserAgent inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS build_tx .IX Subsection "build_tx" .Vb 4 \& my $tx = $ua\->build_tx(GET => \*(Aqexample.com\*(Aq); \& my $tx = $ua\->build_tx(PUT => \*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->build_tx(PUT => \*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->build_tx(PUT => \*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Generate Mojo::Transaction::HTTP object with "tx" in Mojo::UserAgent::Transactor. .PP .Vb 4 \& # Request with custom cookie \& my $tx = $ua\->build_tx(GET => \*(Aqhttps://example.com/account\*(Aq); \& $tx\->req\->cookies({name => \*(Aquser\*(Aq, value => \*(Aqsri\*(Aq}); \& $tx = $ua\->start($tx); \& \& # Deactivate gzip compression \& my $tx = $ua\->build_tx(GET => \*(Aqexample.com\*(Aq); \& $tx\->req\->headers\->remove(\*(AqAccept\-Encoding\*(Aq); \& $tx = $ua\->start($tx); \& \& # Interrupt response by raising an error \& my $tx = $ua\->build_tx(GET => \*(Aqhttp://example.com\*(Aq); \& $tx\->res\->on(progress => sub ($res) { \& return unless my $server = $res\->headers\->server; \& $res\->error({message => \*(AqOh noes, it is IIS!\*(Aq}) if $server =~ /IIS/; \& }); \& $tx = $ua\->start($tx); .Ve .SS build_websocket_tx .IX Subsection "build_websocket_tx" .Vb 2 \& my $tx = $ua\->build_websocket_tx(\*(Aqws://example.com\*(Aq); \& my $tx = $ua\->build_websocket_tx( \*(Aqws://example.com\*(Aq => {DNT => 1} => [\*(Aqv1.proto\*(Aq]); .Ve .PP Generate Mojo::Transaction::HTTP object with "websocket" in Mojo::UserAgent::Transactor. .PP .Vb 12 \& # Custom WebSocket handshake with cookie \& my $tx = $ua\->build_websocket_tx(\*(Aqwss://example.com/echo\*(Aq); \& $tx\->req\->cookies({name => \*(Aquser\*(Aq, value => \*(Aqsri\*(Aq}); \& $ua\->start($tx => sub ($ua, $tx) { \& say \*(AqWebSocket handshake failed!\*(Aq and return unless $tx\->is_websocket; \& $tx\->on(message => sub ($tx, $msg) { \& say "WebSocket message: $msg"; \& $tx\->finish; \& }); \& $tx\->send(\*(AqHi!\*(Aq); \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS delete .IX Subsection "delete" .Vb 4 \& my $tx = $ua\->delete(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->delete(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->delete(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->delete(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`DELETE\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`DELETE\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->delete(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS delete_p .IX Subsection "delete_p" .Vb 1 \& my $promise = $ua\->delete_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "delete", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->delete_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS get .IX Subsection "get" .Vb 4 \& my $tx = $ua\->get(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->get(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->get(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->get(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`GET\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`GET\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->get(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS get_p .IX Subsection "get_p" .Vb 1 \& my $promise = $ua\->get_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "get", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->get_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS head .IX Subsection "head" .Vb 4 \& my $tx = $ua\->head(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->head(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->head(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->head(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`HEAD\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`HEAD\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->head(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS head_p .IX Subsection "head_p" .Vb 1 \& my $promise = $ua\->head_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "head", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->head_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS options .IX Subsection "options" .Vb 4 \& my $tx = $ua\->options(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->options(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->options(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->options(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`OPTIONS\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`OPTIONS\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->options(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS options_p .IX Subsection "options_p" .Vb 1 \& my $promise = $ua\->options_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "options", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->options_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS patch .IX Subsection "patch" .Vb 4 \& my $tx = $ua\->patch(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->patch(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->patch(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->patch(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`PATCH\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`PATCH\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->patch(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS patch_p .IX Subsection "patch_p" .Vb 1 \& my $promise = $ua\->patch_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "patch", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->patch_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS post .IX Subsection "post" .Vb 4 \& my $tx = $ua\->post(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->post(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->post(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->post(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`POST\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`POST\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->post(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS post_p .IX Subsection "post_p" .Vb 1 \& my $promise = $ua\->post_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "post", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->post_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS put .IX Subsection "put" .Vb 4 \& my $tx = $ua\->put(\*(Aqexample.com\*(Aq); \& my $tx = $ua\->put(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => \*(AqContent!\*(Aq); \& my $tx = $ua\->put(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => form => {a => \*(Aqb\*(Aq}); \& my $tx = $ua\->put(\*(Aqhttp://example.com\*(Aq => {Accept => \*(Aq*/*\*(Aq} => json => {a => \*(Aqb\*(Aq}); .Ve .PP Perform blocking \f(CW\*(C`PUT\*(C'\fR request and return resulting Mojo::Transaction::HTTP object, takes the same arguments as "tx" in Mojo::UserAgent::Transactor (except for the \f(CW\*(C`PUT\*(C'\fR method, which is implied). You can also append a callback to perform requests non-blocking. .PP .Vb 2 \& $ua\->put(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq} => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS put_p .IX Subsection "put_p" .Vb 1 \& my $promise = $ua\->put_p(\*(Aqhttp://example.com\*(Aq); .Ve .PP Same as "put", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 5 \& $ua\->put_p(\*(Aqhttp://example.com\*(Aq => json => {a => \*(Aqb\*(Aq})\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS start .IX Subsection "start" .Vb 1 \& my $tx = $ua\->start(Mojo::Transaction::HTTP\->new); .Ve .PP Perform blocking request for a custom Mojo::Transaction::HTTP object, which can be prepared manually or with "build_tx". You can also append a callback to perform requests non-blocking. .PP .Vb 3 \& my $tx = $ua\->build_tx(GET => \*(Aqhttp://example.com\*(Aq); \& $ua\->start($tx => sub ($ua, $tx) { say $tx\->result\->body }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS start_p .IX Subsection "start_p" .Vb 1 \& my $promise = $ua\->start_p(Mojo::Transaction::HTTP\->new); .Ve .PP Same as "start", but performs all requests non-blocking and returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 6 \& my $tx = $ua\->build_tx(GET => \*(Aqhttp://example.com\*(Aq); \& $ua\->start_p($tx)\->then(sub ($tx) { \& say $tx\->result\->body; \& })\->catch(sub ($err) { \& warn "Connection error: $err"; \& })\->wait; .Ve .SS websocket .IX Subsection "websocket" .Vb 2 \& $ua\->websocket(\*(Aqws://example.com\*(Aq => sub {...}); \& $ua\->websocket(\*(Aqws://example.com\*(Aq => {DNT => 1} => [\*(Aqv1.proto\*(Aq] => sub {...}); .Ve .PP Open a non-blocking WebSocket connection with transparent handshake, takes the same arguments as "websocket" in Mojo::UserAgent::Transactor. The callback will receive either a Mojo::Transaction::WebSocket or Mojo::Transaction::HTTP object, depending on if the handshake was successful. .PP .Vb 11 \& $ua\->websocket(\*(Aqwss://example.com/echo\*(Aq => [\*(Aqv1.proto\*(Aq] => sub ($ua, $tx) { \& say \*(AqWebSocket handshake failed!\*(Aq and return unless $tx\->is_websocket; \& say \*(AqSubprotocol negotiation failed!\*(Aq and return unless $tx\->protocol; \& $tx\->on(finish => sub ($tx, $code, $reason) { say "WebSocket closed with status $code." }); \& $tx\->on(message => sub ($tx, $msg) { \& say "WebSocket message: $msg"; \& $tx\->finish; \& }); \& $tx\->send(\*(AqHi!\*(Aq); \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP You can activate \f(CW\*(C`permessage\-deflate\*(C'\fR compression by setting the \f(CW\*(C`Sec\-WebSocket\-Extensions\*(C'\fR header, this can result in much better performance, but also increases memory usage by up to 300KiB per connection. .PP .Vb 3 \& $ua\->websocket(\*(Aqws://example.com/foo\*(Aq => { \& \*(AqSec\-WebSocket\-Extensions\*(Aq => \*(Aqpermessage\-deflate\*(Aq \& } => sub {...}); .Ve .SS websocket_p .IX Subsection "websocket_p" .Vb 1 \& my $promise = $ua\->websocket_p(\*(Aqws://example.com\*(Aq); .Ve .PP Same as "websocket", but returns a Mojo::Promise object instead of accepting a callback. .PP .Vb 12 \& $ua\->websocket_p(\*(Aqwss://example.com/echo\*(Aq)\->then(sub ($tx) { \& my $promise = Mojo::Promise\->new; \& $tx\->on(finish => sub { $promise\->resolve }); \& $tx\->on(message => sub ($tx, $msg) { \& say "WebSocket message: $msg"; \& $tx\->finish; \& }); \& $tx\->send(\*(AqHi!\*(Aq); \& return $promise; \& })\->catch(sub ($err) { \& warn "WebSocket error: $err"; \& })\->wait; .Ve .SH DEBUGGING .IX Header "DEBUGGING" You can set the \f(CW\*(C`MOJO_CLIENT_DEBUG\*(C'\fR environment variable to get some advanced diagnostics information printed to \&\f(CW\*(C`STDERR\*(C'\fR. .PP .Vb 1 \& MOJO_CLIENT_DEBUG=1 .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .