boson-php/http-body-decoder
Lightweight HTTP request body decoder for PHP 8.4+. Decode incoming payloads by Content-Type (e.g., JSON, form data) into usable values, designed for Boson apps but usable standalone via Composer.
Install the package via Composer:
composer require boson-php/http-body-decoder
Register the decoder in your middleware to parse incoming request bodies:
use Boson\HttpBodyDecoder\Decoder;
use Boson\HttpBodyDecoder\DecoderFactory;
public function handle($request, Closure $next)
{
$decoder = DecoderFactory::create();
$decoder->decode($request->getContent());
// Now access parsed data via $request->attributes
return $next($request);
}
DecoderFactory: Creates decoders for JSON, form data, or multipart.Decoder: Core interface for parsing raw request bodies.Request extension: Automatically attaches parsed data to Laravel's Request object.Middleware Integration:
// app/Http/Middleware/DecodeBody.php
public function handle($request, Closure $next)
{
$decoder = DecoderFactory::create();
$decoder->decode($request->getContent());
// Attach parsed data to request attributes
$request->attributes->set('parsed_body', $decoder->getParsedData());
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\DecodeBody::class,
];
Controller Access:
public function store(Request $request)
{
$parsedData = $request->attributes->get('parsed_body');
// Use $parsedData directly
}
Decoder for domain-specific formats (e.g., XML).ValidatesRequests to validate parsed data:
public function update(Request $request)
{
$this->validate($request, [
'parsed_body.field' => 'required|string',
]);
}
decode() multiple times on the same raw body.Content-Type headers match the decoder type (e.g., application/json for JSON).$request->getContent() to verify input.DecoderException for malformed input (e.g., invalid JSON).Boson\HttpBodyDecoder\DecoderInterface for new formats:
class CustomDecoder implements DecoderInterface
{
public function decode(string $body): void
{
// Custom logic
}
}
DecoderFactory to inject custom decoders:
DecoderFactory::addDecoder('custom', CustomDecoder::class);
$this->app->bind(DecoderFactory::class, function () {
return DecoderFactory::create();
});
application/json by default; explicitly set Content-Type for other formats.upload_max_filesize and post_max_size for large file uploads.How can I help you explore Laravel packages today?