How to grab data using fetch() API POST method in PHP?

How to grab data using fetch() API POST method in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your POST Request Isn't Populating $_GET

The core issue in your example stems from a fundamental mismatch between how data is sent and how it is being received on the server side. You are attempting to send data using the fetch() API with the POST method, but your PHP script is configured to listen for GET parameters via $_GET['x']. These two mechanisms operate in completely different request streams.

When you execute a GET request, parameters like ?x=hello are appended to the URL and parsed into the $_GET superglobal array in PHP. However, when you switch to a POST request using fetch(), that data is moved to the request body. Consequently, $_GET remains empty because no query string was provided, and your current check isset($_GET['x']) will fail every time.

To successfully retrieve the data sent via fetch() in a POST request, you must access the correct superglobal: $_POST. This array holds key-value pairs from form submissions or JSON payloads that are part of the body payload, not the URL query string.

Here is how you should modify your PHP code to capture the data correctly:

<?php
if (isset($_POST['x'])) {
    $post = $_POST['x'];
    echo $post;
}
?>

However, there is a significant nuance depending on how you are sending the data from your JavaScript. In your snippet, you set body: x where x is a raw string "hello". Browsers and HTTP clients generally expect structured data formats when sending JSON over POST requests. If your PHP endpoint expects JSON, you should parse that incoming stream instead of relying on $_POST, which handles form-encoded data (application/x-www-form-urlencoded).

If you send the body as a raw string without specifying a content type, the server might not automatically populate $_POST correctly depending on the configuration. The robust approach is to explicitly set the Content-Type header in your fetch() call to application/json. This signals to the server that the body contains JSON data.

On the PHP side, when receiving JSON, you should use the file_get_contents('php://input') function or the json_decode method on the raw input stream. This bypasses the automatic parsing of form variables and gives you direct access to the request body as a string, which you can then convert into a usable PHP object or array.

<?php
// Method 1: Handling JSON specifically
$input = file_get_contents('php://input');

if ($input !== false && !empty($input)) {
    $data = json_decode($input, true); // Decode to associative array
    
    if (is_array($data) && isset($data['x'])) {
        echo $data['x'];
    }
}
?>

This approach is often preferred in modern API development because it decouples the input format from the superglobals. It allows you to handle complex data structures or nested objects that might not fit neatly into $_POST.

If you are building an application using a framework like Laravel, handling incoming JSON payloads is even more straightforward using the request object, which automatically decodes JSON based on headers. You can access the value via $request->get('x') or $request->input('x'), ensuring type safety and cleaner code regardless of whether the data arrived as application/json or form-encoded content.

In summary, always match your receiving logic to the HTTP method and content type you are sending. If you send a POST request with JSON, decode the input stream; if you send form data, use $_POST. Mixing these concepts leads to the exact error you encountered where the data simply never made it into the expected variable.

Our network