Why $_POST Remains Empty When Sending Form Data via Axios
When debugging a cross-language API call, it is easy to assume that setting Content-Type: application/x-www-form-urlencoded automatically transforms your payload into something PHP's $_POST superglobal can parse. In the scenario described, the developer correctly switched from axios.post() to the lower-level axios({}) syntax to explicitly control headers. They set the header to application/x-www-form-urlencoded, populated the data object with fields like json and type, and observed that the server could read the raw body via file_get_contents("php://input"). However, $_POST remained empty.
The root cause lies in a mismatch between the payload structure and the parsing logic of PHP's default request handler. Even though the Content-Type header is correct, the actual string sent inside the HTTP body matters immensely. When you pass an object directly to Axios's data property:
axios({
method: 'post',
url,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: {
json: "someValue",
type: "myType"
}
})
Axios does not blindly trust the Content-Type header you provide. By default, when Axios encounters an object in the data field and detects a Content-Type of application/x-www-form-urlencoded, it attempts to serialize that object. However, the specific behavior depends heavily on the version of Axios and its internal configuration regarding serialization logic.
In many cases, if Axios detects that you have manually set the header to application/x-www-form-urlencoded, it expects a string in the data property or relies on its default serializer which might not format the output exactly as PHP expects for superglobal parsing without explicit encoding. More critically, if your environment or a specific version of Axios defaults to JSON serialization when an object is passed, it will ignore your manual header setting and send a JSON string instead. PHP's $_POST array only populates when the incoming body matches the format defined by the Content-Type: application/x-www-form-urlencoded and the body content consists of key-value pairs separated by & and name-value pairs separated by =.
If you inspect the network tab or use file_get_contents("php://input"), you are seeing the raw string. If that string looks like {"json":"value","type":"myType"}, PHP treats it as JSON, not form data, leaving $_POST empty regardless of the header declaration. This is a common pitfall: relying on the header alone to define how PHP parses the body without ensuring the actual byte stream matches that format.
To resolve this and populate $_POST, you must explicitly serialize the data into a query-string format before sending it. You can achieve this by using URLSearchParams. This ensures the payload strictly adheres to the RFC standards for form data, making it compatible with PHP's native parsing mechanisms without needing to read from php://input manually.
Here is how you should adjust the Axios request:
const params = new URLSearchParams();
params.append('json', 'someValue');
params.append('type', 'myType');
axios({
method: 'post',
url,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: params // Pass the stringified search params here
})
By constructing a URLSearchParams object and passing it to the data property, Axios will automatically stringify it into a query-string format (e.g., json=someValue&type=myType). When this specific string reaches PHP with the matching header, $_POST['json'] and $_POST['type'] will be populated correctly.
This approach eliminates the ambiguity of relying on Axios's internal serialization heuristics. It guarantees that the raw HTTP body contains exactly what PHP expects for form data parsing, ensuring seamless integration between frontend JavaScript clients and backend PHP applications handling traditional form submissions.
