NAME

Async::MicroserviceReq - async microservice request class

SYNOPSIS

my $this_req  = Async::MicroserviceReq->new(
    method     => $plack_req->method,
    headers    => $plack_req->headers,
    content    => $plack_req->content,
    path       => $plack_req->path_info,
    params     => $plack_req->parameters,
    static_dir => $self->static_dir,
);

...

my $plack_handler_sub = sub {
    my ($plack_respond) = @_;
    $this_req->plack_respond($plack_respond);
...

DESCRIPTION

This is an object created for each request handled by Async::Microservice. It is passed to all request handling functions as the first argument and it provides request information and response helper methods.

ATTRIBUTES

method
headers
path
params
plack_respond
static_dir
base_url
want_json
content
json_content

METHODS

text_plain(@text_lines)

Send text plain response.

respond($status, $headers, $payload)

Send a PSGI/Plack response.

$headers must be an array reference of header key/value pairs.

If $payload is not a reference, it is sent as plain text by default. When the request Accept header allows JSON and no explicit Content-Type header is already present, plain scalar payloads are wrapped automatically as JSON:

{ "data": "..." }

For error statuses ($status >= 400), scalar payloads are wrapped as:

{ "error": { err_status => ..., err_msg => ... } }

If $payload is a reference, it is serialized as JSON. When JSONP is enabled and a valid callback parameter is present, the response is emitted as application/javascript instead of application/json.

redirect($location_path)

Send redirect.

static_ft($file_name, $content_cb)

Send static file, can be updated/modified using optional callback.

get_pending_req

Returns number of currently pending async requests.