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 Laravel Package

sabre/http

sabre/http is a lightweight toolkit for working with HTTP in PHP. It wraps superglobals and output functions into extendable, mockable Request and Response objects, making it easier to read request data and send headers/body consistently across your app.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing via Composer: composer require sabre/http. The core entry point is Sabre\HTTP\Sapi, used to build Request and Response objects from PHP’s native superglobals and output mechanisms. Instantiate the request once at application bootstrap:

$request = Sabre\HTTP\Sapi::getRequest();

Then inject RequestInterface everywhere instead of relying on $_GET, $_POST, or $_SERVER. For responses, construct a Response, configure it, and dispatch it once at the end:

$response = new Sabre\HTTP\Response();
$response->setBody('OK');
Sabre\HTTP\Sapi::sendResponse($response);

Start by reading examples/ in the repo — especially reverseproxy.php — to see real-world usage.

Implementation Patterns

  • Dependency Injection: Pass RequestInterface and ResponseInterface through layers (controllers, services, middleware). Avoid calling Sapi::getRequest() multiple times.
  • Decorator Pattern: Extend behavior without modifying base classes. Wrap the core request/response with your own decorators (e.g., AuthenticatedRequest, LoggedResponse) to add domain-specific methods like isLoggedIn() or logTimestamp().
  • Testing: Inject Mockery or custom RequestInterface implementations into tests — no reliance on global state.
  • Client Usage: Use HTTP\Client for lightweight API calls. Combine with on('error') hooks to implement retries or auth injection. Prefer async mode for batch external calls (sendAsync() + wait()).
  • Reverse Proxy: Build simple proxies by cloning the incoming request, rewriting URLs/headers, forwarding via Client, and echoing the response back with Sapi::sendResponse().

Gotchas and Tips

  • Stream Handling: getBodyAsStream() returns a non-seekable resource — read only once. Convert to string only once via getBodyAsString() (subsequent calls may fail or return empty).
  • Header Case Sensitivity: Headers are case-insensitive per RFC — use getHeader('Content-Type'), not getHeader('content-type'). But setHeader() preserves original casing.
  • Decorator Limitations: Decorators don’t inherit type-hinted logic expecting concrete Request — ensure your APIs accept only RequestInterface.
  • SAPI-Only APIs: Sapi::getRequest() only reads from superglobals. In CLI or non-web contexts, construct Request manually or use Sapi::getRequestFromServer() for unit testing.
  • Async Caveats: In async mode, $client->wait() is blocking — avoid mixing sync/async unless you buffer results.
  • Deprecation Warning: sabre/http is mature but low activity; consider symfony/http-foundation for larger frameworks unless you need its simpler, decorator-friendly design.
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
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
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation