phasync
Streams and files / MultipartStreamInterface

Interface

MultipartStreamInterface

A request body stream that carries a multipart/form-data payload.

Introduction#

HttpClient::sendRequest sends self::getContentType(), boundary included, as the Content-Type header of a request whose body implements this interface, so the caller sets none. Implement it for a body that is built elsewhere than MultipartStream.

Class synopsis#

interface MultipartStreamInterface extends StreamInterface, \Stringable {
    /* Methods */
    public MultipartStreamInterface::getContentType(): string
}

Methods#

MethodDescription
getContentTypeReturn the value of the Content-Type header, with the boundary parameter.

Examples#

Example 1The Content-Type comes with the body
use phasync\HttpClient\HttpClient;
use phasync\HttpClient\MultipartStream;
use phasync\HttpClient\MultipartStreamInterface;
use phasync\Psr\Request;

$base = require __DIR__ . '/../server.php';

$body = new MultipartStream(['comment' => 'looks good'], 'b0undary');
var_dump($body instanceof MultipartStreamInterface);

// sendRequest() sets Content-Type, with the boundary, from the body
$request  = Request::create('POST', "$base/echo")->withBody($body);
$echo     = json_decode((string) (new HttpClient())->sendRequest($request)->getBody(), true);
echo $echo['headers']['content-type'], "\n";
echo json_encode($echo['form']), "\n";
Output
bool(true)
multipart/form-data; boundary=b0undary
{"comment":"looks good"}
Example 2
$body = new MultipartStream(['title' => 'Report', 'file' => \fopen('report.pdf', 'r')]);
$client->post('https://example.com/upload', $body); // Content-Type: $body->getContentType()

See also#