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

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

Install the package via Composer:

composer require boson-php/http-body-decoder

First Use Case: Decoding Request Bodies in Laravel Middleware

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);
}

Key Entry Points

  • 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.

Implementation Patterns

Workflow: Handling Incoming Requests

  1. 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,
    ];
    
  2. Controller Access:

    public function store(Request $request)
    {
        $parsedData = $request->attributes->get('parsed_body');
        // Use $parsedData directly
    }
    

Integration Tips

  • PSR-7 Adapters: Works seamlessly with frameworks like Symfony or Slim.
  • Custom Decoders: Extend Decoder for domain-specific formats (e.g., XML).
  • Validation: Chain with Laravel's ValidatesRequests to validate parsed data:
    public function update(Request $request)
    {
        $this->validate($request, [
            'parsed_body.field' => 'required|string',
        ]);
    }
    

Gotchas and Tips

Pitfalls

  • Double Parsing: Avoid calling decode() multiple times on the same raw body.
  • Memory Usage: Large multipart files may consume significant memory; stream processing is recommended.
  • Content-Type Mismatch: Ensure Content-Type headers match the decoder type (e.g., application/json for JSON).

Debugging

  • Log Raw Data: Temporarily log $request->getContent() to verify input.
  • Decoder Errors: Check DecoderException for malformed input (e.g., invalid JSON).

Extension Points

  • Custom Decoders: Implement Boson\HttpBodyDecoder\DecoderInterface for new formats:
    class CustomDecoder implements DecoderInterface
    {
        public function decode(string $body): void
        {
            // Custom logic
        }
    }
    
  • Factory Extensions: Override DecoderFactory to inject custom decoders:
    DecoderFactory::addDecoder('custom', CustomDecoder::class);
    
  • Laravel Service Provider: Bind the factory to Laravel's container for DI:
    $this->app->bind(DecoderFactory::class, function () {
        return DecoderFactory::create();
    });
    

Configuration Quirks

  • Default Decoder: Uses application/json by default; explicitly set Content-Type for other formats.
  • Multipart Limits: Configure PHP’s upload_max_filesize and post_max_size for large file uploads.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky