Axios posts as JSON object, how to change

Axios posts as JSON object, how to change

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Axios Posts as JSON Object, How to Change

When you send a request using axios.post, the data is transmitted in the body of the HTTP payload. By default, Axios serializes this data as a JSON object. This behavior is standard for modern web applications but creates a specific friction point when working with legacy PHP backends that rely on $_REQUEST.

The issue arises because $_REQUEST aggregates data from GET, POST, and COOKIE sources, but it parses only form-encoded data by default (application/x-www-form-urlencoded). When Axios sends JSON, the server receives the raw string in the request body. Since PHP does not automatically parse this into $_REQUEST, your variables like firstName and lastName remain invisible to $_REQUEST. Attempting to access them there results in null or empty values.

Changing the Content-Type header is a common misconception that often leads developers down a dead end. You might try setting the header to application/x-www-form-urlencoded, but this fundamentally changes how Axios serializes your data object. If you force that header, Axios will expect a flat array of key-value pairs rather than a nested JSON structure. While this makes the data visible to $_REQUEST, it breaks the ability to send complex objects or nested data structures easily.

The most robust solution is to configure the PHP application to handle raw input streams instead of relying on superglobals like $_REQUEST or $_POST. In modern PHP applications, specifically those using frameworks like Laravel, the standard approach is to read from php://input. This stream contains the exact bytes sent in the request body, regardless of the Content-Type header.

Here is how you can implement this in a PHP controller:

$rawData = file_get_contents('php://input');

// Parse the JSON manually or use json_decode
$data = json_decode($rawData, true);

if (json_last_error() === JSON_ERROR_NONE) {
    $firstName = $data['firstName'] ?? null;
    $lastName = $data['lastName'] ?? null;
    
    // Proceed with logic using $firstName and $lastName
}

This method bypasses the limitations of $_REQUEST entirely. It ensures that whatever Axios sends—whether it is a simple object or a deeply nested structure—is parsed correctly by your backend logic. This pattern is widely adopted in high-volume API development because it decouples the client's serialization format from the server's internal variable parsing.

If you are building on top of Laravel, this approach is even more streamlined. You can inject the request into your controller and access the raw JSON directly:

public function store(Request $request)
{
    $data = json_decode($request->getContent(), true);
    
    // Validate and process $data['firstName']
}

The Request facade in Laravel automatically handles Content-Type detection. If the header is application/json, accessing $request->input('firstName') will still return null because the default input method expects form data. However, calling json_decode on $request->getContent() guarantees you get the associative array back from Axios.

There are trade-offs to consider. Using php://input means you lose automatic CSRF protection if your application relies on cookies for state management without additional checks. Furthermore, if the client sends malformed JSON, json_decode will return null, so you must always handle decoding errors explicitly in production code to prevent silent failures.

For legacy systems where you cannot modify the PHP logic to use streams, you would need a server-side middleware that intercepts requests with a Content-Type of application/json and populates $_REQUEST or specific $_POST keys before your controller logic runs. This is generally less recommended as it couples your application tightly to Axios's serialization behavior.

In summary, the friction between Axios default JSON output and PHP's $_REQUEST is a mismatch in data interpretation layers. The cleanest resolution involves shifting the PHP backend to consume raw input streams and manually decoding the JSON payload. This approach offers greater flexibility for handling complex data structures and aligns with modern API development standards.

Our network