Dev.to WebDev 🛠 Dev 👁 0 📖 8 min read

HTTP QUERY in Laravel: The Cart Reads We Moved to POST

In early 2024 I moved four cart endpoints from GET to POST. None of them writes anything. They read the cart, count totals, check if checkout is possible and load delivery schedules. The URL just got too long, and POST w

In early 2024 I moved four cart endpoints from GET to POST. None of them writes anything. They read the cart, count totals, check if checkout is possible and load delivery schedules. The URL just got too long, and POST was the only verb that takes a body.

This year HTTP got a new method for exactly this case, and Laravel already supports it. So I went back to that cart to see if it is time to undo the hack.

The Cart That Reads With POST

In a project I work on, the frontend asks the backend about the items a customer selected in the cart. The API has no sessions, so the backend doesn't remember that selection: every read carries the list of item uuids, and a big cart sends hundreds of them:

final class CartItemIndexRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'filter' => ['array', 'min:1', 'max:500'],
            'filter.item_uuid.*' => ['required', 'string', 'max:100'],
        ];
    }
}

In February 2024 the reads were plain GET routes, and the cart itself was one URI with two verbs:

// BEFORE
Route::get('', [CartController::class, 'index'])->name('index');
Route::post('', [CartController::class, 'store'])->name('store');

Route::get('total', [CartController::class, 'totalAmounts'])->name('total.amounts');
Route::get('checkout-possibility', [CartController::class, 'checkoutPossibility'])->name('checkout.possibility');
Route::get('delivery-schedules', [CartController::class, 'deliverySchedules'])->name('delivery.schedules');

One commit later all four reads were POST. And index could not stay on '', because POST /cart was already taken by store. So the read got a made-up path, POST /cart/index. Later adding an item got add-item. This is the relevant part of the cart route file today:

Route::post('index', [CartController::class, 'index'])->name('index');
Route::post('add-item', [CartController::class, 'storeItem'])->name('add-item');
Route::put('update/{uuid}', [CartController::class, 'update'])->name('update');
Route::delete('delete', [CartController::class, 'destroy'])->name('destroy');

Route::post('total-amounts', [CartController::class, 'totalAmounts'])->name('total-amounts');
Route::post('checkout-possibility', [CartController::class, 'checkoutPossibility'])->name('checkout-possibility');
Route::post('delivery-schedules', [CartController::class, 'deliverySchedules'])->name('delivery-schedules');

Look at index and add-item. Same verb, and the paths are there only to keep the two apart. One reads, one writes, and the verb can't tell you which. Neither can anything between the browser and Laravel. POST means "this may change something", so retry and cache policies treat it as unsafe: no automatic retry, no caching.

Where the GET Actually Broke

The commit message says nothing about why. I remember the symptom: with a bigger cart the requests just failed in the browser. The uuids are long, and every one goes into the query string as filter[item_uuid][]=....

I rebuilt the request against stock nginx (1.31, default config) to get a real number:

uuids   url length   status
125     8164         200
130     8489         414
500     32539        414

nginx keeps the request line in an 8 KB buffer (large_client_header_buffers 4 8k). Past that it answers 414 Request-URI Too Large, and PHP never sees the request. So the cart did not break at 500 items. It broke at about 130.

The 414 page also has no Access-Control-Allow-Origin header. Laravel adds the CORS headers, but Laravel never ran. A frontend on another origin then gets a CORS or network error instead of a readable 414. I'm fairly sure that is the "browser error" we saw in 2024, but I can't check the old server config anymore.

You can raise the buffer. We didn't, and it only moves the wall: load balancers, proxies and CDNs all have their own URL limits.

What QUERY Changes

In June 2026 the IETF published RFC 10008. It adds QUERY: a method that is safe and idempotent like GET, but carries a request body like POST. It's the first new general-purpose HTTP method since PATCH in 2010.

In CRUD terms nothing new appears. It's still the R: a read, where the question goes in the body instead of the URL.

Laravel added it in two steps. 13.19 brought the HTTP client and test helpers, 13.35 brought the route. With it the cart routes can go back to plain CRUD:

// AFTER (Laravel 13.35+)
Route::prefix('cart')->name('cart.')->group(function (): void {
    // Create, Read, Update, Delete
    Route::post('', [CartController::class, 'storeItem'])->name('store');
    Route::query('', [CartController::class, 'index'])->name('index');
    Route::put('{uuid}', [CartController::class, 'update'])->name('update');
    Route::delete('', [CartController::class, 'destroy'])->name('destroy');

    // Reads about the cart
    Route::query('total-amounts', [CartController::class, 'totalAmounts'])->name('total-amounts');
    Route::query('checkout-possibility', [CartController::class, 'checkoutPossibility'])->name('checkout-possibility');
    Route::query('delivery-schedules', [CartController::class, 'deliverySchedules'])->name('delivery-schedules');
});

QUERY /cart and POST /cart share a URI and Laravel routes them to different actions, the same way GET and POST did in 2024. The verb says read or write again, not the path.

The controller and the form request stay the same. I checked the routing on Laravel 13.44 behind nginx and php-fpm, with debug closures in place of the controller:

curl -X QUERY http://localhost:8097/api/v2/cart \
  -H 'Content-Type: application/json' -H 'Accept: application/json' \
  -d '{"filter":{"item_uuid":["a","b"]}}'
{"action":"index","filter":{"item_uuid":["a","b"]}}

A POST to the same URI answered {"action":"store"}, and a GET got 405. $request->input('filter') reads a JSON body and a form-encoded body the same way.

If another Laravel app calls this endpoint, the client has a matching method. It sends JSON by default:

$response = Http::query('https://shop.test/api/v2/cart', [
    'filter' => ['item_uuid' => $uuids],
]);

And feature tests get query() and queryJson():

$this->queryJson('/api/v2/cart', ['filter' => ['item_uuid' => $uuids]])
    ->assertOk();

One thing Laravel did not add: a query action for resource controllers. PR #62107 proposed it for Route::resource() and make:controller --resource, and it was closed without a merge. So resource routes don't get another action. QUERY is something you opt into per route.

The Traps I Hit Trying It

php artisan serve answers 501

My first try was php artisan serve and curl. The answer:

501 Not Implemented
Request method not supported.

That's PHP's built-in web server (8.4 here) rejecting the method before Laravel boots. Test QUERY behind nginx and php-fpm (Sail, Herd, Docker). nginx passed it through with zero extra config.

Green tests prove nothing about CSRF

On 13.x a QUERY route in the web group is CSRF-checked like a POST. The reasoning is in PR #61797: QUERY is safe by the spec, but your handler is still just app code with a request body.

My curl to a web QUERY route without a token got 419. The same request in a feature test with query() got 200, because Laravel skips the CSRF check while running tests. So the suite can't tell you anything here.

Who actually gets the 419? Not a normal page on the same domain: Laravel 13 lets a request through when the browser sends Sec-Fetch-Site: same-origin, and axios sends the X-XSRF-TOKEN cookie value anyway. It's curl, other servers, other origins and old browsers. Routes in api.php have no session, so they skip the check, unless you use Sanctum SPA auth, which turns it back on for your own frontend.

Everything between the browser and Laravel

I haven't tested these on a real CDN or WAF, so treat them as a checklist, not results:

  • QUERY isn't a CORS-safelisted method, so every cross-origin browser call sends a preflight first. Laravel's default config/cors.php allows '*' methods, so check it only if you narrowed the list.
  • Caches key on the URL. For QUERY the body has to be part of the key, so don't expect your CDN to cache it yet.
  • A WAF, load balancer or API gateway with a method allowlist can answer 405 before PHP runs.
  • HTML forms send only GET and POST. QUERY is for fetch, axios and server-to-server calls.
  • Nobody can copy the URL and see the same result. The RFC lets the server answer with a Location URL you can GET later, but you have to build that yourself.

My Rule of Thumb

Is QUERY a good addition? Yes. It fixes a real lie: reads that pretend to be writes. But it doesn't replace GET.

Request Verb
A few short filters, link should be shareable GET
Big or nested filter, called from JS or another server QUERY
Anything that changes state POST / PUT / PATCH / DELETE
Plain HTML form GET or POST

For our cart, the four reads are going to QUERY. I thought that meant waiting for a Laravel 13 upgrade, since the project is still on 11. Turns out it doesn't. Route::query() is just a shortcut, and Route::match() takes the method on 11.57 too:

// Laravel 11
Route::match(['QUERY'], '', [CartController::class, 'index'])->name('index');

Same nginx and php-fpm setup, same result: QUERY reaches index, POST reaches store, GET gets 405. What 11 doesn't have is Http::query() and queryJson(); in tests, $this->json('QUERY', ...) does the job. So the framework isn't what's blocking us. The frontend and every proxy in front of the app are, and that's the next thing to check.

TL;DR

  • POST for reads works, but retry and cache policies treat those calls as writes, and the route file stops telling reads from writes.
  • A GET with a long filter dies at about 8 KB on default nginx, and a cross-origin frontend sees a CORS error instead of the 414.
  • Route::query() (13.35), Http::query() and queryJson() (13.19) make QUERY easy in Laravel 13, and Route::match(['QUERY'], ...) already works on 11.
  • Don't test it with php artisan serve (501), and don't treat a green test as proof your CSRF setup is right.

💡 Before you switch, run curl -X QUERY against staging. Your server, proxy and CDN have to agree.

Author's Note

Thanks for sticking around!
Find me on dev.to, linkedin, or you can check out my work on github.

Laravel, after the happy path.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.