Skip to content

Requests

Every request your application receives is a Pam\Http\Request, built by the PAM runtime and passed to your route handler. It is a small, immutable value object: the method, the path, the query string, the headers, the raw body and the route parameters. Nimbus adds helpers for form fields, cookies and Laravel-style input access on top of it.

Most of the time you will not read raw input at all. Declare a form request or a typed input and receive validated data instead; this page covers everything underneath.

Type-hint Pam\Http\Request in a route closure or a controller action:

routes.php
use Pam\Http\Request;
$router->get('/search', fn (Request $request) => ['q' => $request->getQuery('q', '')]);
app/Controllers/UserController.php
use Pam\Http\Request;
public function show(Request $request, string $id): array
{
return ['id' => $id, 'expand' => $request->getQuery('expand')];
}

Route parameters can be declared alongside the request, in any order; Nimbus matches them by name.

$request->method; // "GET"
$request->path; // "/users/7" (no query string)

method is always upper-case. To build absolute URLs, combine the scheme and host from $_SERVER:

$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

REQUEST_SCHEME describes the connection PAM accepted. Behind a TLS-terminating proxy it is http; read the proxy’s X-Forwarded-Proto header (only from proxies you trust) if you need the public scheme.

getHeader() is case-insensitive and joins repeated headers with , . headers() returns every header as lower-case names mapped to lists of values:

$agent = $request->getHeader('User-Agent');
$tenant = $request->getHeader('x-tenant', 'public');
foreach ($request->headers() as $name => $values) {
// $name is lower-case, $values is a list of strings
}

To read a bearer token, use Nimbus\Http\RequestData::bearerToken():

use Nimbus\Http\RequestData;
$token = RequestData::bearerToken($request); // "abc123" for "Authorization: Bearer abc123"

The client address is $_SERVER['REMOTE_ADDR']. PAM resolves it from X-Forwarded-For only when the connection comes from one of the trustedProxies, walking from the right and stopping at the first address that is not a trusted proxy. Without trusted proxies, it is the peer address of the TCP connection.

$ip = $_SERVER['REMOTE_ADDR'];

If you need the same logic for an address list of your own, Nimbus\Http\ClientIpResolver implements it:

use Nimbus\Http\ClientIpResolver;
$ip = (new ClientIpResolver(['10.0.0.0/8']))->resolve($request);
$page = (int) $request->getQuery('page', 1);
$filters = $request->query(); // ['page' => '2', 'sort' => 'name']

Query values are strings (or arrays, for tags[]=a&tags[]=b). Cast or validate them before use.

json() decodes the body into arrays and throws JsonException when the body is not valid JSON:

$router->post('/echo', fn (Request $request) => ['received' => $request->json()]);

The raw bytes are always available through body():

$signature = hash_hmac('sha256', $request->body(), $secret);

URL-encoded and multipart bodies are parsed into $_POST for every request. Nimbus’ FormData reads the same fields from the request object, and treats a JSON object body the same way:

use Nimbus\Http\FormData;
$fields = FormData::parse($request); // ['name' => 'Lamp', 'price' => '2500']
$name = FormData::field($request, 'name'); // "Lamp", or null

Code ported from Laravel can use Nimbus\Http\RequestData. It merges the body over the query string (a body field wins over a query parameter with the same name) and accepts dot notation:

use Nimbus\Http\RequestData;
$router->post('/legacy', fn (Request $request) => [
'all' => RequestData::all($request),
'name' => RequestData::input($request, 'user.name'),
'has' => RequestData::has($request, ['user.name', 'missing']),
'token' => RequestData::bearerToken($request),
]);
Terminal window
$ curl -X POST 'http://127.0.0.1:3000/legacy?page=2' -H 'authorization: Bearer abc123' \
-H 'content-type: application/json' -d '{"user":{"name":"Alice"}}'
{"all":{"page":"2","user":{"name":"Alice"}},"name":"Alice","has":false,"token":"abc123"}
Method Returns
RequestData::all($request) Body fields over query parameters.
RequestData::input($request, $key = null, $default = null) One value by dot path, or everything.
RequestData::has($request, $keys) Whether every key is present, even with a null value.
RequestData::filled($request, $key) Whether the key is present and not empty.
RequestData::only($request, ...$keys) Only the listed keys that are present.
RequestData::bearerToken($request) The bearer token, or null.

Read cookies with Nimbus\Http\RequestCookies, or from $_COOKIE:

use Nimbus\Http\RequestCookies;
$router->get('/prefs', fn (Request $request) => [
'theme' => RequestCookies::fromRequest($request)->get('theme', 'light'),
]);
Terminal window
$ curl -H 'cookie: theme=dark' http://127.0.0.1:3000/prefs
{"theme":"dark"}

When the application registers EncryptCookiesMiddleware, inject RequestCookies into the action instead; it then holds the decrypted values. Writing cookies is covered in Responses.

Multipart uploads are streamed to disk by the runtime and exposed through $_FILES, exactly as in PHP-FPM. Move the temporary file before the request ends:

routes.php
use Nimbus\Http\HttpException;
use Nimbus\Http\ProblemCode;
$router->post('/avatar', function (): array {
$file = $_FILES['avatar'] ?? null;
if ($file === null || $file['error'] !== UPLOAD_ERR_OK) {
throw new HttpException(422, ProblemCode::ValidationFailed, 'Upload an avatar.');
}
move_uploaded_file($file['tmp_name'], '/var/lib/acme/avatars/' . bin2hex(random_bytes(8)));
return ['name' => $file['name'], 'size' => $file['size'], 'fields' => $_POST];
});
Terminal window
$ curl -F '[email protected];type=image/png' -F 'caption=Me' http://127.0.0.1:3000/avatar
{"name":"me.png","size":6,"fields":{"caption":"Me"}}

Uploads follow PHP’s upload_max_filesize, post_max_size and max_file_uploads settings, and the whole body is bounded by the maxBodyBytes listener option (2 MiB by default); larger bodies get 413 Payload Too Large. For large files, have clients upload directly to object storage with a signed URL instead of streaming through your workers.

Every request carries a correlation id in $_SERVER['PAM_REQUEST_ID'] and a W3C trace context in $_SERVER['PAM_TRACEPARENT']. When the client sends X-Request-Id, PAM reuses it as the label. The same id appears in every log line the request writes, so you can find all of them in one search; see Logging.

$requestId = $_SERVER['PAM_REQUEST_ID'];