References Getting Started Getting Started Architecture Architecture Application Application Window Window WebView WebView Deployment Deployment Components Components Framework Integrations Framework Integrations Examples Examples Community Community github GitHub github Get Started arrow_up_right
Getting Started Getting Started Architecture Architecture Application Application Window Window WebView WebView Deployment Deployment Components Components Framework Integrations Framework Integrations Examples Examples Community Community

Components

HTTP URI HTTP Body Decoder HTTP Static Provider PHP Globals Provider OS Info CPU Info Assembly Weak Types Getting Started Architecture Application Window WebView Deployment Components Framework Integrations Examples Community

HTTP Body Decoder

Provides the ability to decode the request body and obtain useful information from there based on the request information.

This component is not included by default in the boson-php/runtime and must be installed separately.

Installation

Via Composer:

composer require boson-php/http-body-decoder

Requirements:

  • PHP ^8.4

Basic Usage

The main entry point is the BodyDecoderFactory class, which automatically selects the appropriate decoder based on the request:

use Boson\Component\Http\Body\BodyDecoderFactory;
use Boson\Component\Http\Body\FormUrlEncodedDecoder;
use Boson\Component\Http\Body\MultipartFormDataDecoder;
use Boson\Component\Http\Request;

// Create decoders factory
$factory = new BodyDecoderFactory([
    // Decodes "application/x-www-form-urlencoded" requests
    new FormUrlEncodedDecoder(),
    // Decodes "multipart/form-data" requests
    new MultipartFormDataDecoder(),
]);

// Decode request
$decodedBody = $factory->decode( $bosonRequest );

Supported Decoders

Form URL Encoded

The FormUrlEncodedDecoder handles application/x-www-form-urlencoded content type:

use Boson\Component\Http\Body\FormUrlEncodedDecoder;
use Boson\Component\Http\Request;

$decoder = new FormUrlEncodedDecoder();

$request = new Request(
    method: 'POST',
    headers: ['Content-Type' => 'application/x-www-form-urlencoded'],
    body: 'name=John&age=30'
);

$decodedBody = $decoder->decode(new Request(
    method: 'POST',
    headers: ['Content-Type' => 'application/x-www-form-urlencoded'],
    body: 'name=John&age=30'
));

//
// Expected output:
//
// array:2 [
//   "name" => "John"
//   "age" => "30"
// ]
//

Multipart Form Data

The MultipartFormDataDecoder handles multipart/form-data content type, including file uploads:

use Boson\Component\Http\Body\MultipartFormDataDecoder;
use Boson\Component\Http\Request;

$decoder = new MultipartFormDataDecoder();

$decodedBody = $decoder->decode(new Request(
    method: 'POST',
    headers: [
        'Content-Type' => 'multipart/form-data;' 
            . ' boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
    ],
    // Note: The HTTP protocol uses \r\n delimiters
    body: str_replace(["\r\n", "\n"], "\r\n", <<<'BODY'
        ------WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Disposition: form-data; name="name"
        
        John
        ------WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Disposition: form-data; name="file"; filename="test.txt"
        Content-Type: text/plain
        
        Hello World
        ------WebKitFormBoundary7MA4YWxkTrZu0gW--
        BODY),
));

// 
// Expected output:
//
// array:2 [
//   "name" => "John"
//   "file" => "Hello World"
// ]
//

Creating Custom Decoders

You can create custom decoders by implementing the SpecializedBodyDecoderInterface:

The SpecializedBodyDecoderInterface interface means that the decoder will respond to a specific specialized request type (see method isDecodable()).

use Boson\Component\Http\Body\SpecializedBodyDecoderInterface;
use Boson\Component\Http\Request;

class JsonBodyDecoder implements SpecializedBodyDecoderInterface
{
    public function decode(RequestInterface $request): array
    {
        return (array) (\json_decode($request->body, true) ?? []);
    }

    public function isDecodable(RequestInterface $request): bool
    {
        return $request->headers->first('content-type')
            === 'application/json';
    }
}

$factory = new BodyDecoderFactory([
    new JsonBodyDecoder(),
    // ... other decoders
]);

Error Handling

The factory handles decoder errors gracefully:

use Boson\Component\Http\Body\BodyDecoderFactory;
use Boson\Component\Http\Request;

$factory = new BodyDecoderFactory($decoders);

// If no decoder is suitable or decoding fails
$decodedBody = $factory->decode(new Request(
    method: 'POST',
    headers: ['Content-Type' => 'unknown/type'],
    body: 'invalid data'
));

// 
// Expected output:
//
// [] (empty array)
//
github discord telegram Get started Documentation Contribution Guide License Release Notes BOSON PHP © 2025. All Rights Reversed.