symfony/http-kernel · error · LogicException
You cannot pass non-empty bodies as the Mime component is…
Error message
You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".
What it means
HttpClientKernel::getBody() converts non-GET/HEAD request bodies into a Mime TextPart for the outgoing HTTP request. Converting a non-empty body requires symfony/mime (AbstractPart); without it, passing a non-empty body throws this LogicException.
Solutions
- Run composer require symfony/mime
- Or send GET/HEAD requests only, or requests with empty bodies
- Or provide the body as a pre-built AbstractPart yourself (still requires mime)
Example fix
// before (throws for POST with content)
$client->request('POST', '/api', ['body' => 'data']);
// after
composer require symfony/mime
$client->request('POST', '/api', ['body' => 'data']); Defensive patterns
Strategy: validation
Validate before calling
if ($request->getContent() !== '' && !class_exists(\Symfony\Component\Mime\Part\AbstractPart::class)) {
throw new \LogicException('symfony/mime required for non-empty bodies; run composer require symfony/mime');
}
$client->request($method, $uri, $options); Try / catch
try {
$client->request('POST', $uri, ['body' => $payload]);
} catch (\LogicException $e) {
$logger->error($e->getMessage());
} Prevention
- composer require symfony/mime whenever symfony/http-client is used with request bodies
- Avoid sending request bodies on GET/HEAD
- Check composer.lock includes transitive mime dependency
When it happens
Trigger: Sending a request with a non-empty body (POST/PUT/PATCH with content, or form data) through HttpClientKernel when symfony/mime is not installed. GET/HEAD requests are unaffected (return null).
Common situations: symfony/http-client installed but symfony/mime not pulled in as a dependency; minimal composer install that omits transitive requirements in older setups.
Understand the failure class
Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.
Related errors
- You cannot use " " as the HttpClient component is not…
- Cannot evaluate Expression for controllers since no…
- The "symfony/serializer" component is required to use the "#
- Request payload contains invalid "form" data.
- Unsupported format: " ".
AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13).
Data as JSON: /api/errors/cea2ba9068c69b00.
Report an issue: GitHub.
Appendix: source
Thrown at HttpClientKernel.php:86
try {
return new Response($response->getContent(!$catch), $response->getStatusCode(), $headers);
} catch (\TypeError) {
// BC with Symfony < 8.1
$response = new Response($response->getContent(!$catch), $response->getStatusCode());
$response->headers = $headers;
return $response;
}
}
private function getBody(Request $request): ?AbstractPart
{
if (\in_array($request->getMethod(), ['GET', 'HEAD'], true)) {
return null;
}
if (!class_exists(AbstractPart::class)) {
throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');
}
if ($content = $request->getContent()) {
return new TextPart($content, 'utf-8', 'plain', '8bit');
}
$fields = $request->request->all();
foreach ($request->files->all() as $name => $file) {
$fields[$name] = DataPart::fromPath($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType());
}
return new FormDataPart($fields);
}
private function getHeaders(Request $request): array
{
$headers = [];
foreach ($request->headers as $key => $value) {View on GitHub (pinned to aa3a39d728)