PromptHub
Laravel Intermediate 19 views

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

19

Related Errors