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

See the message coming from the server #2

Closed
americanslon opened this issue Jan 22, 2020 · 7 comments
Closed

See the message coming from the server #2

americanslon opened this issue Jan 22, 2020 · 7 comments

Comments

@americanslon
Copy link

Is it possible to see the response message coming from the server and if so could to you post an example?

I am trying to use this to have on spot where I place the "error" toast so I need to be able to see when the request returns the error.

Thanks!

@khamang
Copy link

khamang commented Jan 23, 2020

One solution is to change the SendAsync method to:

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = null;
            try
            {
                BeforeSend?.Invoke(this, request);
                response = await (SendAsyncMethod.Invoke(Handler, new object[] { request, cancellationToken }) as Task<HttpResponseMessage>);
                return response;
            }
            finally
            {
                AfterSend?.Invoke(this, response);
            }
        }

and the eventhandlers to

/// <summary>
/// Occurs before a HTTP request sending.
/// </summary>
public event EventHandler<HttpRequestMessage> BeforeSend;

/// <summary>
/// Occurs after received a response of a HTTP request. (include it wasn't succeeded.)
/// </summary>
public event EventHandler<HttpResponseMessage> AfterSend;

@americanslon
Copy link
Author

I took this suggestion. Sorta seems that's how it should be in the first place. Makes this interceptor significantly more useful, unless I am not understanding the usefulness of EventArgs e

@jsakamoto
Copy link
Owner

Thank you for nice feedback! 👍

I published "Blazor WebAssembly (client-side) HttpClient Interceptor" v.6.1.0 that include the new feature suggested by @khamang !

See also: https://github.com/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor/#the-arguments-of-event-handler

@TheSwerik
Copy link

TheSwerik commented Sep 2, 2020

So this should be fixed? I still get an error if I want to access the response content in the AfterSend.
It says in the Service that makes the http.PostAsync cant access the body because it is locked

@jsakamoto
Copy link
Owner

@TheSwerik Thank you for your reporting.

So this should be fixed? I still get an error ...

This thread is not an error report, this is a new feature proposal thread.
I never knew it has a problem that you reported now.

I'll try to reproduce that problem later, and I'll try to fix it if I could reproduce it.

@TheSwerik
Copy link

Oh I thought this fits here, I will create a new and more detailed issue

@jsakamoto
Copy link
Owner

@TheSwerik Thanks!

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