Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseReceived event doesn't serialize correctly #37959

Closed
viniciushss-interstar opened this issue Jul 9, 2021 · 8 comments
Closed

ResponseReceived event doesn't serialize correctly #37959

viniciushss-interstar opened this issue Jul 9, 2021 · 8 comments

Comments

@viniciushss-interstar
Copy link

  • Laravel Version: 8.49.2
  • PHP Version: 8.0.6
  • Database Driver & Version: pgsql

Description:

When listening to the event Illuminate\Http\Client\Events\ResponseReceived, the HTTP response body is inaccessible, besides being in the payload.

Steps To Reproduce:

1 - Make a HTTP request, for example, with a test route in routes/web.php:

Route::get('/test', function () {
    $response = Http::post('https://httpbin.org/post', [
        'name' => 'Steve',
        'role' => 'Network Administrator',
    ]);
    return response()->json($response->json());
});

2 - Create listener (e.g. app/Listeners/ResponseReceivedTestListener.php):

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Client\Events\ResponseReceived;

class ResponseReceivedTestListener implements ShouldQueue
{

    public function handle(ResponseReceived $event)
    {
        $body = $event->response->getBody();
        echo $body;
    }
}

3 - Register the listener in app/Providers/EventServiceProvider.php:

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        ResponseReceived::class => [
            ResponseReceivedTestListener::class,
        ]
    ];
}

4 - Go to http://127.0.0.1:8000/test or invoke the HTTP request created in the 1º step;

5 - In the failed_jobs table:

TypeError: fseek(): Argument #1 ($stream) must be of type resource, int given in /vendor/guzzlehttp/psr7/src/Stream.php:210
Stack trace:
#0 /vendor/guzzlehttp/psr7/src/Stream.php(210): fseek(0, 0, 0)
#1 /vendor/guzzlehttp/psr7/src/Stream.php(81): GuzzleHttp\Psr7\Stream->seek(0)
#2 /app/Listeners/ResponseReceivedTestListener.php(14): GuzzleHttp\Psr7\Stream->__toString()
#3 /vendor/laravel/framework/src/Illuminate/Events/CallQueuedListener.php(107): App\Listeners\ResponseReceivedTestListener->handle(Object(Illuminate\Http\Client\Events\ResponseReceived))
#4 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Events\CallQueuedListener->handle(Object(Illuminate\Foundation\Application))
#5 /vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#7 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#8 /vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#9 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call(Array)
#10 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Illuminate\Events\CallQueuedListener))
#11 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Events\CallQueuedListener))
#12 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(120): Illuminate\Bus\Dispatcher->dispatchNow(Object(Illuminate\Events\CallQueuedListener), false)
#14 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}(Object(Illuminate\Events\CallQueuedListener))
#15 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Events\CallQueuedListener))
#16 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(70): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Events\CallQueuedListener))
#18 /vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#19 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(428): Illuminate\Queue\Jobs\Job->fire()
#20 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(378): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#21 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(329): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#22 /vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(117): Illuminate\Queue\Worker->runNextJob('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#23 /vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#24 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#25 /vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#26 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#27 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#28 /vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#29 /vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call(Array)
#30 /vendor/symfony/console/Command/Command.php(299): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#31 /vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#32 /vendor/symfony/console/Application.php(978): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /vendor/symfony/console/Application.php(295): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /vendor/symfony/console/Application.php(167): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 /vendor/laravel/framework/src/Illuminate/Console/Application.php(92): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 /artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#38 {main}

I'm not experienced enough to tell what the problem exactly is, but I can see the response body in the payload column. I've also tried force rewind the stream cursor but it didn't fix:

$body->rewind();
echo $body->getContents();

and

echo $event->response->getBody()->__toString();
@ajuchacko
Copy link

Hi @viniciushss-interstar,

You actually have a question here not an issue. For which is what there is some forums already created. So you can ask this types questions there.

https://laravel.com/docs/8.x/events

   ....
    event(new ResponseReceived($response->json()));
    return response()->json($response->json());


    class ResponseReceived {

     public _construct($response) {
         $this->response = $response;
     }

     }

@driesvints
Copy link
Member

@ajuchacko91 I know you're trying to help but we'd appreciate it if you wouldn't act as a maintainer of this repository. Please only try to help directly with the issue.

@driesvints
Copy link
Member

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

@ajuchacko
Copy link

@ajuchacko91 I know you're trying to help but we'd appreciate it if you wouldn't act as a maintainer of this repository. Please only try to help directly with the

Ok, no problem 😂 I am not planning to take over your job

@viniciushss-interstar
Copy link
Author

Hi @driesvints,

I'm sorry, I've created this issue because I think the ResponseReceived event is not behaving as stated in the documentation.

The documentation says:

[...] Likewise, the ResponseReceived event contains a $request property as well as a $response property which may be used to inspect the Illuminate\Http\Client\Response instance. [...]

But as I described in the OP, the $response property in this event is not returning the body content as it should. I think there's three possibilities: 1) I could be doing something wrong; 2) This is a expected behavior or 3) This is not expected and is a bug.

Since this event is was introduced in the last release three days ago (PRs #37917 and #37572), maybe it could be a bug or a expected but undocumented behavior?

@semsphy
Copy link

semsphy commented Aug 22, 2021

Hi @driesvints,

I'm sorry, I've created this issue because I think the ResponseReceived event is not behaving as stated in the documentation.

The documentation says:

[...] Likewise, the ResponseReceived event contains a $request property as well as a $response property which may be used to inspect the Illuminate\Http\Client\Response instance. [...]

But as I described in the OP, the $response property in this event is not returning the body content as it should. I think there's three possibilities: 1) I could be doing something wrong; 2) This is a expected behavior or 3) This is not expected and is a bug.

Since this event is was introduced in the last release three days ago (PRs #37917 and #37572), maybe it could be a bug or a expected but undocumented behavior?

I'm facing the same problem, do you have any solution to that? Thanks.

@ajuchacko
Copy link

Hi @semsphy,

what laravel version are you using? and how can I reproduce this issue?

@semsphy
Copy link

semsphy commented Aug 22, 2021

@ajuchacko91 , i'm using laravel v8.55.0 and the way to reproduce this; is simple by listening to this event Illuminate\Http\Client\Events\ResponseReceived with your listener and in your listener handle method just info($event->response->json())); OR you just followed the step of @viniciushss-interstar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants