Samuel A
Samuel A

Reputation: 56

Request Data returned with JSON response

Hey

When sending a request to our Laravel API via POST method the request JSON data is sometimes returned in the JSON response, which results in an invalid response. See below for an example. The strange thing is if we restart the docker container the expected response is given for a bit, and then after a while, it goes back to adding the request data to the response.

I've spent a bit trying to find solutions but nothing I find works. So I'm hoping that someone here can help. From what I've found it seems like it's a nginx issue, and I've tried to disable cache and proxy request buffering but the issue still comes back.

Nginx Location Config

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        proxy_cache off;
        proxy_no_cache 1;
        proxy_cache_bypass 1;
        if_modified_since off;
        expires off;
        etag off;
        proxy_request_buffering off;
    }

Our setup

PHP Image: php:8.1.0-fpm-alpine3.15
Nginx Image: nginx:stable-alpine
Laravel Version: 8.78.1

Request

{
    "first_name": "John",
    "last_name": "Doe"
}

Controller

public function store(SomeRequest $request)
{
    $createdModel = SomeModel::create($request->validated());

    return response()->json([
        'model' => $createdModel
    ]);
}

Response

{
    "first_name": "John",
    "last_name": "Doe"
}
{
    "model" => {
        "id" : 1,
        "first_name": "John",
        "last_name": "Doe"
    }
}

Upvotes: 0

Views: 619

Answers (1)

Samuel A
Samuel A

Reputation: 56

This issue was fixed by disallowing external connections to PHP-FPM (Port 9000).

We tracked down the issue and found that after a while PHP changed allow_url_include from Off to On. By blocking the port, it stopped changing the value by itself.

Upvotes: 0

Related Questions