
Chased a stupid problem all day today. Long story short: I had a simple Laravel controller that was...
title: Testing Laravel JSON requests published: true description: tags: laravel,php,json
Chased a stupid problem all day today. Long story short: I had a simple Laravel controller that was trying to just echo back the inputs from a JSON request body. It's not enough to write a test like the following.
class MyTest extends TestCase {
function test_example() {
$this->withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
])->post('echo', ['foo' => 'bar'])->assertJson(['foo' => 'bar']);
}
}
The controller tries to read $request->all(), which is empty! That's because of how the Laravel test framework constructs request objects. It doesn't care that I provided those headers to indicate that there was a JSON body to draw from. You have to use postJson instead of post for that, and then you don't need those headers, either.
function test_example() {
$this->postJson('echo', ['foo' => 'bar'])->assertJson(['foo' => 'bar']);
}
The same is true for patch vs. patchJson. So somehow, I've been using the wrong method all this time, and it's never been a problem? I'm shocked and confused.
gemmaI ported the whole Gemma-4 family — E2B, E4B, 12B, 31B, and the 26B-A4B MoE — to run on...
communityHey DEV, I'm Tobore. Let's actually connect. I've been on here for a while now, mostly writing and...
ai(yep, kinda clickbait, just for the funsies 😊) At the beginning of the year, I relaunched my...
aiMy laptop was sitting idle with the fan at full tilt. Nothing was running that I knew of. The culprit...
githubactionsI Built a Thing! TL;DR — Google Gemini-based Pull Request reviews and Issue Triaging for...
aiI've been hearing the word "harness" thrown around a lot lately. I assumed it just meant "the IDE" or...
Workflows from the Neura Market marketplace related to this DeepSeek resource