0

Per the strapi documentation, the way to filter a collection is to use a square bracket syntax in the request query, eg

GET /api/users?filters[username][$eq]=John

In the first instance when implementing this in my PHP application, I reached for the mbvienasbaitas/strapi-php-client library which is linked from the strapi site. This however relies on PHPs http_build_query function which encodes the square brackets in the request.

The Strapi API returns a 500 error when the square brackets are encoded.

I've tried to manually make the same request without encoded square brackets, and it works.

I resorted to using Guzzle instead to make the request, as I assumed I would have more control over this. Unfortunately I encountered the same issue. Guzzle seemingly doesn't allow you to prevent url encoding of the square brackets in a query string.

Is there a way to do this without resorting to CURL? I find it odd that I seem to be the only person on the internet asking this question in the context of strapi (that I can find, anyway).

Nate
  • 511
  • 4
  • 14
  • 2
    Then the strapi HTTP service is broken in that it is not compliant with the HTTP spec, as there should be no functional difference if the braces are encoded or not. Because of this your only choice is to write your own bespoke code to create some purposefully-wonky URIs for this badly-implemented service. – Sammitch May 23 '23 at 21:24
  • 1
    You could try either this, https://github.com/guzzle/guzzle/issues/1758#issuecomment-581111911, or the method below that. – CBroe May 24 '23 at 06:08
  • And `file_get_contents` also does no additional URL encoding of its own, when you use it to make HTTP requests. – CBroe May 24 '23 at 06:09
  • so doing something like this doesn't work `$client->request('GET', '/api/users', ['query' => ["filters[username][{$eq}]" => "John"]]);` – bhucho Jun 05 '23 at 00:40
  • are you sure the encoded query is correct as I don't think someone would reject encoded brackets – bhucho Jun 05 '23 at 00:42

0 Answers0