MalformedPayloadException
The request payload or session data is corrupted or invalid.
Explanation
MalformedPayloadException is thrown when Laravel receives a request payload or session data that cannot be parsed correctly. This often happens with invalid JSON in API requests, corrupted session data, or tampered cookie content. The exception can also occur when encrypted cookies or tokens are modified by the client. In newer Laravel versions, this may also surface when the session encryption key changes and old sessions can't be decrypted.
Common Causes
- Invalid JSON payload
- APP_KEY changed without clearing sessions
- Corrupted session data
- Tampered encrypted cookies
- Missing Content-Type header
Solution
Ensure all API requests send valid JSON with the correct Content-Type header. Clear browser cookies and session data when changing the APP_KEY. Verify that APP_KEY is properly set in the .env file and hasn't been changed recently. Check that the session driver is configured correctly. If using encrypted cookies, ensure the encryption key hasn't changed. For API requests, validate the JSON payload before processing.
Code Example
// API request must have valid JSON
// Wrong: sending form data to a JSON-only endpoint
fetch('/api/users', {
method: 'POST',
body: 'name=John', // This may cause MalformedPayloadException
});
// Correct: send JSON with proper headers
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({ name: 'John' }),
});
// If APP_KEY changes, clear all sessions
php artisan cache:clear
php artisan config:clear
# Clear Redis: redis-cli FLUSHDB
# Clear database sessions: php artisan session:table && php artisan migrate
Error Information
Language
php
Framework
laravel
Difficulty
Intermediate
Views
276
Related Errors
PHP
Parse Error: Syntax Error
PHP detected a syntax error in the source code that prevents parsing.
Database
Database Connection Refused or Timeout
The database server is not reachable or took too long to respond.
Database
Table Doesn't Exist
The SQL query references a table that doesn't exist in the database.
PHP
Cannot Use String Offset as Array
The code tries to access a string using array syntax, treating it as an array.