Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Thatdoorsajar's avatar

Laravel 5.1 returns 500 error when called by Guzzle (5.3/6.0)

I have created a very simple API with L5.1, something along the lines of:

use Api\Models\Contact;

Route::get('api/contacts', function() {
    return Contact::all();
});

I will call this APIONE. The URI from APIONE returns correct json to the browser and via curl request on the command line. However, when calling from another app with Guzzle (with all 5.2, 5.3 & 6.0) I get an Internal Server 500. I have another VERY similar API app built on L4.2, let's call this APITWO and when called via Guzzle (5.3 &OR 6.0) it returns correct json to my API consuming app leading me to conclude that this is a problem / change with how L5.1 handles outside requests.

Anyone experiencing similar or know something about Laravel 5.1 I dont? Thanks

0 likes
3 replies
igorblumberg's avatar
Level 7

It might be some CSRF issue.

Laravel 4.2 did not have middlewares, and on 5.1 the CSRF middleware is "on" by default. Since your code works on the browser and curl but not from another app it might be some issue with that.

Just disable the CSRF middleware to be sure (go to App->Http->Kernel and remove 'App\Http\Middleware\VerifyCsrfToken' from the $middleware array)

ps.: If this is the error that might not be the best solution to your use case. You might want to generate a csrf_token on your client and send it on the request.

1 like
ijpatricio's avatar

I was facing same problem, driving crazy beacuse of many variables here, upgrade to 5.1 LTS, php 5.6, guzzle also...

Life saving, @igorblumberg!!

Should have thought of it, sometimes it needs someone out of the equation, to figure out something so simple is the solution!

Instead of removing from the middleware, you could go inside the App\Http\Middleware\VerifyCsrfToken.php,

and add to the $except array, the pattern for route that you dont want CSRF verification, say

protected $except = [ 'your/uri/route/*' ]

Happy coding!!

1 like

Please or to participate in this conversation.