Illuminate\Support\Facades\Http request body defaults to empty array []
(footgun)
#53678
Unanswered
bcheidemann-kmbal
asked this question in
Ideas
Replies: 1 comment 3 replies
-
The absence of any body in a POST request that is really unintuitive. An empty array as a request body clearly indicates that you intentionally send no data to the server. For some rare cases where no body is preferable, you've offered the workaround |
Beta Was this translation helpful? Give feedback.
3 replies
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Laravel Version
11.18.1
PHP Version
8.3.9
Database Driver & Version
n/a
Description
If no request body is specified when using the
Illuminate\Support\Facades\Http
post
,patch
,put
, ordelete
methods, it defaults to an empty array, which is JSON encoded as[]
. This is unintuitive, and can lead to unexpected behaviour or bugs.Example of Resulting Bug
Assume a REST API exists which is authenticated via a HMAC authentication header. Such an API may choose to hash either the request body and/or the request path if the request body is empty. A client calling this API with an empty body should only hash the request path. If the client is a Laravel application, the implementor may (as I did) assume that the default body is empty, and accidentally call the API with a non-empty body. This will result in the API and Laravel client application computing different hashes for the same request.
Workarounds
Proposed Fix
By default, the request body should be empty. The
$data
argument should also be made nullable.Steps To Reproduce
Open two shell sessions.
In the first shell, run:
In the second shell,
cd
into a Laravel project and run:Observe that in the first shell
"[]"
gets logged.Beta Was this translation helpful? Give feedback.
All reactions