Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Http Server Request Laravel Package

httpsoft/http-server-request

PSR-7/PSR-17 friendly ServerRequest implementation and helpers for building HTTP server requests in PHP. Lightweight, standards-based request object with convenient access to headers, cookies, query params, body, uploaded files, and server params.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer

    composer require httpsoft/http-server-request
    
  2. Create a ServerRequest from superglobals (most common use case)

    use HttpSoft\ServerRequest\ServerRequestFactory;
    
    $request = (new ServerRequestFactory())->createServerRequestFromGlobals();
    
  3. Use it in middleware
    Since it’s PSR-7 compliant, plug it directly into any PSR-15/PSR-15-compatible middleware stack (e.g., with Nyholm/psr7, Relay, or custom dispatcher):

    $request = (new ServerRequestFactory())->createServerRequestFromGlobals();
    $response = $middleware($request, $response);
    

πŸ’‘ First-tip: Start with ServerRequestFactory::createServerRequestFromGlobals() β€” it handles all the superglobal parsing (including $_COOKIE, $_FILES, $_SERVER) with PSR-7 normalization.


Implementation Patterns

πŸ“¦ Middleware Integration

Use the request as the standard interface for handling requests in middleware:

$app = function ($request) {
    $method = $request->getMethod();
    $path   = $request->getUri()->getPath();
    return new JsonResponse(['path' => $path, 'method' => $method]);
};

πŸ” Custom Request Construction

Build requests programmatically (e.g., for testing or CLI-initiated HTTP flows):

$request = (new ServerRequestFactory())->createServerRequest(
    'POST',
    '/api/upload',
    [
        'Content-Type' => 'multipart/form-data; boundary=----WebKit',
        'Authorization' => 'Bearer xyz'
    ],
    $_COOKIE,
    $_GET,
    $_POST,
    $_FILES
);

πŸ“ File Upload Handling

Access uploaded files with normalized PSR-7 structures:

$uploadedFiles = $request->getUploadedFiles();
$file = $uploadedFiles['avatar'] ?? null;
if ($file instanceof UploadedFileInterface && $file->getError() === UPLOAD_ERR_OK) {
    $file->moveTo('/var/www/uploads/' . $file->getClientFilename());
}

🧩 Parse JSON Body Automatically

Since the package supports body parsing:

if ($request->hasHeader('Content-Type') && strpos($request->getHeaderLine('Content-Type'), 'application/json') !== false) {
    $parsedBody = $request->getParsedBody(); // e.g., ['name' => 'Jane', 'age' => 30]
}

πŸ’‘ Pattern tip: Combine with httpsoft/psr7 for full PSR-7/18 compatibility in non-framework apps.


Gotchas and Tips

🚫 Super/global Parsing Quirks

  • $_COOKIE is parsed only if setCookieParse() isn’t overridden β€” default behavior follows PHP’s mb_parse_str()-style rules, but httpsoft/http-server-request parses cookies more robustly (e.g., handles nested arrays). Double-check complex cookie structures.
  • $_FILES structure must match PHP’s array format β€” if mocking in tests, use this format:
    $_FILES = [
        'avatar' => [
            'name' => 'me.jpg',
            'type' => 'image/jpeg',
            'tmp_file' => '/tmp/php123',
            'error' => 0,
            'size' => 12345,
        ]
    ];
    

πŸ›  Debugging & Testing

  • Use __toString() on requests to debug headers/body (debug-only β€” not for prod):
    error_log((string) $request);
    
  • For unit tests, prefer manual construction over createServerRequestFromGlobals() to avoid side effects.

πŸ”§ Extension Points

  • You can subclass ServerRequestFactory to override parsing behavior (e.g., custom body parsers or header normalization).
  • Support for PSR-17 (factories) is partial β€” prefer this package’s own factory, as it handles more edge cases.

⚠️ Potential Pitfall

  • Parsed body is immutable: Calling withParsedBody() returns a new instance β€” always reassign:
    // ❌ Wrong: ignored
    $request->withParsedBody(['foo' => 'bar']);
    
    // βœ… Correct
    $request = $request->withParsedBody(['foo' => 'bar']);
    

πŸ’‘ Pro tip: For apps not using a framework, combine this with httpsoft/psr17-factory to satisfy full PSR-17/18 expectations across the stack.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport