Error:
Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Zend\Diactoros\Response given, called in /app/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php on line 61
When ThrottleRequests middleware is used with PSR-7 and PHP 7.1 with strict_mode enabled, addHeaders method cant be used because has type Response on first param:
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
PSR-7 use Zend\Diactoros\Response, and ThrottleRequests uses Symfony\Component\HttpFoundation\Response
ThrottleRequests because we can't rewrite original addHeadersp function.if on addHeaders() and chick if we are using PSR-7 response ($response->headers->add to $response->withAddedHeaderaddHeadersp with GrahamCampbell/Laravel-ThrottleIf the option 2 is right, I would be happy to do a PR.
I found the problem. Laravel, automatically converts PSR-7 responses to Synfony responses.
:warning: But PSR-7 instance IS CONVERTED ONLY WHEN RESPONSE COMES FROM A ROUTE OR CONTROLLER :warning:
Then, in my case, response comes from an Error Handler. The problem also occurs if PSR-7 RESPONSE is generated in a middleware.
// some middleware or error handler
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
$http_foundation_factory = new HttpFoundationFactory();
$laravel_esponse = $http_foundation_factory->createResponse($psr_response);
Check your middlewares: return a response is required on every handle() methods .
I would appreciate some insight on this. I'm getting the same error but the stack is only Laravel stuff. I didn't write any of the concerned middleware. Works fine locally but not on Staging. I have no idea how to debug this:
[2019-10-10 09:51:08] local.ERROR: Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php on line 62 {"userId":29855,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() must be an instance of Symfony\\Component\\HttpFoundation\\Response, null given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php on line 62 at /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:149)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(62): Illuminate\\Routing\\Middleware\\ThrottleRequests->addHeaders(NULL, 120, 115)
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 120, '1')
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#19 /var/www/html/public/index.php(53): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
I found the problem. Laravel, automatically converts PSR-7 responses to Synfony responses.
⚠️ But PSR-7 instance IS CONVERTED ONLY WHEN RESPONSE COMES FROM A ROUTE OR CONTROLLER ⚠️
Then, in my case, response comes from an Error Handler. The problem also occurs if PSR-7 RESPONSE is generated in a middleware.
Solution
// some middleware or error handler use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; $http_foundation_factory = new HttpFoundationFactory(); $laravel_esponse = $http_foundation_factory->createResponse($psr_response);Solution 2
Check your middlewares: return a response is required on every
handle()methods .
Hi I am very new to Laravel Where I need to add this
Hi!
Search on your code some use of Symfony\Component\HttpFoundation\Response. If you return some $response with this type, replace with solution.
Just for future readers.... I had this issue and it was due to my middleware not catering for an else.
Changed this:
if (($request->get('tenant_identifier') === $request->user(
)->tenant_identifier) && ($request->get(
'enterprise_identifier'
) === $request->user()->enterprise_identifier)
) {
return $next($request);
To this:
if (($request->get('tenant_identifier') === $request->user(
)->tenant_identifier) && ($request->get(
'enterprise_identifier'
) === $request->user()->enterprise_identifier)
) {
return $next($request);
} else {
abort(403);
}
My code is also causing such an issue. Can anyone help me to solve this?
Thanks in advance.
Code is here:
public function store(Request $request , User $seller)
{
$rules =[
'name'=>'required',
'description'=>'required',
'quantity'=>'required|integer|min:1',
'image'=>'required|image'
];
$this->validate($request,$rules);
$data = $request->all();
$data['status'] = Product::PRODUCT_UNAVAILABLE;
$data['image'] = 'images/1.jpg';
$data['seller_id'] = $seller->id;
$product = Product::create($data);
$product->save();
return $this->showOne($product);
}
Ali did you find the solution. I think we are watching same tutorial on Udemy and got same error. I am still looking for the solution. If you find it please let me know.
If you get this error while using an API, try to return JSON response in the failedValidation method inside the FormRequest you use to validate the request as follwoing:
protected function failedValidation(Validator $validator)
{
$errors = $validator->errors()->toArray();
$info = [
'success' => false,
'message' => 'The given data was invalid.',
'status_code' => 422,
'status_description' => 'The given data was invalid.',
'errors' => $errors
];
$response = new JsonResponse($info, 422);
throw new ValidationException($validator, $response);
}
this approach is working for me
I did the jwt handle on laravel, and the same problem occurred, please help
`
namespace App\Http\Middleware;
use Firebase\JWT\JWT;
use Firebase\JWT\ExpiredException;
use Closure;
use Exception;
class JWTAuth {
public function handle($request, Closure $next, $guard = null)
{
$token = $request->header('Authorization');
$verify = explode(" ", $token);
if ($verify[0] !== "petani") {
return [
'code' => 401,
'error' => 'Token not provided.'
];
}
if (!$token) {
return [
'code' => 400,
'error' => 'Provided token is expired.'
];
}
try {
$credentials = JWT::decode($verify[1], env('JWT_SECRET'), ['HS256']);
} catch(ExpiredException $e) {
return [
'code' => 400,
'error' => 'Token is expired. '
];
} catch(Exception $e) {
return [
'code' => 400,
'error' => 'An error while decoding token.'
];
}
return $next($request);
}
}
?>`
Hi @iqbalnur32 ... Change your return statement to abort functions and you won't get the PSR-7 error.
Therefore instead of returning an array, just abort:
abort(400, 'Token expired') etc..
oke sir iam add method reponse is work
Pada tanggal Sen, 31 Agt 2020 12:46, Keith Mifsud notifications@github.com
menulis:
Hi @iqbalnur32 https://github.com/iqbalnur32 ... Change your return
statement to abort functions and you won't get the PSR-7 error.Therefore instead of returning an array, just abort:
abort(400, 'Token expired') etc..
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/laravel/framework/issues/24730#issuecomment-683562886,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AJNX7ISPYFRKZNHBITADBDDSDM2MZANCNFSM4FIABJMA
.
Most helpful comment
I found the problem. Laravel, automatically converts PSR-7 responses to Synfony responses.
:warning: But PSR-7 instance IS CONVERTED ONLY WHEN RESPONSE COMES FROM A ROUTE OR CONTROLLER :warning:
Then, in my case, response comes from an Error Handler. The problem also occurs if PSR-7 RESPONSE is generated in a middleware.
Solution
Solution 2
Check your middlewares: return a response is required on every
handle()methods .