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

Get actual raw request/response #885

Closed
sonicflare opened this issue Oct 5, 2016 · 25 comments
Closed

Get actual raw request/response #885

sonicflare opened this issue Oct 5, 2016 · 25 comments
Labels

Comments

@sonicflare
Copy link

Hello,
I want to log actual http raw request/response that have been sent out to minimize troubleshooting time. Is there a way to do that?

@dgritsko
Copy link

dgritsko commented Oct 6, 2017

This would be really valuable.

@vk5880
Copy link

vk5880 commented Jul 5, 2018

Is it still not availiable?

@vk5880
Copy link

vk5880 commented Oct 11, 2018

Please make it, so simple...

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Oct 11, 2018

@vk5880 if it is so simple, why won't you make it? I will happily accept your PR.

@stale
Copy link

stale bot commented Jan 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 8, 2020
@repo-ranger
Copy link

repo-ranger bot commented Jan 8, 2020

⚠️ This issue has been marked wontfix and will be closed in 3 days

@repo-ranger repo-ranger bot closed this as completed Jan 11, 2020
@alexeyzimarev
Copy link
Member

Btw, response.RawBytes has always been there...

@seannybgoode
Copy link

@alexeyzimarev - not the case for request.RawBytes though

@ferib
Copy link

ferib commented Sep 3, 2020

@alexeyzimarev those response.RawBytes just seem to represent response.Content, but is there any other method to obtain the actual "raw" data of the request? Data that's not been deserialized?

@alexeyzimarev
Copy link
Member

ok, so it's about the request, right? how deserialisation applies to the request? requests don't get deserialised, it's the opposite.

@ferib
Copy link

ferib commented Sep 3, 2020

@alexeyzimarev Oh okay thanks for your answer, I tried to deserialize before using RestClient.Execute<string>(request) which didn't work, so I ended up using JsonConvert.DeserializeObject<string>(response.Content) to get the "raw" string from the response.

@alexeyzimarev
Copy link
Member

I am not sure why would you are trying to do. Deserialise to a string? The content is already a string, what’s there to deserialise?

@ferib
Copy link

ferib commented Sep 3, 2020

@alexeyzimarev For example, the content I requested was returned as \"Hello\\nWorld\", I didn't like the fact that it decided to add multi quotes to start and end and that it added escape characters to it. All I wanted was the string.

@alexeyzimarev
Copy link
Member

You can just unescape the string instead.

@ferib
Copy link

ferib commented Sep 4, 2020

@alexeyzimarev that's kind of what I'm trying to achieve yes, but don't know of any other way.

@alexeyzimarev
Copy link
Member

https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape?view=netcore-3.1 maybe?

@softlion

This comment has been minimized.

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Dec 12, 2021

@softlion if it's "unacceptable", I would suggest opening a pull request to make it happen. Easy, right? "Contributions for an easier world"...

@softlion

This comment has been minimized.

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Dec 13, 2021

Of course you did. That's how ".NET community" contributes in general, I assume.

@ChaseFlorell
Copy link

ChaseFlorell commented Dec 13, 2021

I'm curious @alexeyzimarev (I don't use RestSharp), can this framework accept custom Delegating Handlers?
I help maintain another project that might help solve this problem.

It seems like any sort of middleware between the HttpClient and RestSharp should be able to capture the details requested by the OP.

https://www.nuget.org/packages/HttpTracer/

@alexeyzimarev
Copy link
Member

@ChaseFlorell thanks, it looks great!

Basically, the most obvious issue was always that RestSharp used the legacy HttpWebRequest, which hides the HttpClient instance inside it. With v107 the issue is gone. People can either use RestClient that instantiates its own HttpClient, or provide either their own HttpClient or HttpMessageHandler. This means that all the HttpClient instrumentation and middleware will work.

But this particular issue is weird as RestSharp was never composing raw request bytes, it was (and is) using what .NET provides for making HTTP calls. As long as you know how to observe HTTP calls, you should be good.

Your library is trivial to connect to v107 I would assume.

@alexeyzimarev
Copy link
Member

Ok, to make things even simpler I added an option to configure the handler for the case when one is not creating the handler themselves. That's how it can be used:

    public MultipartFormDataTests(ITestOutputHelper output) {
        _output = output;
        _server = SimpleServer.Create(RequestHandler.Handle);

        var options = new RestClientOptions(_server.Url) {
            ConfigureMessageHandler = handler => new HttpTracerHandler(handler, new OutputLogger(output), HttpMessageParts.All)
        };
        _client = new RestClient(options);
    }

I added to one of the tests and it works great. Thanks again @ChaseFlorell :)

@wilkovanderveen
Copy link

Ok, to make things even simpler I added an option to configure the handler for the case when one is not creating the handler themselves. That's how it can be used:

    public MultipartFormDataTests(ITestOutputHelper output) {
        _output = output;
        _server = SimpleServer.Create(RequestHandler.Handle);

        var options = new RestClientOptions(_server.Url) {
            ConfigureMessageHandler = handler => new HttpTracerHandler(handler, new OutputLogger(output), HttpMessageParts.All)
        };
        _client = new RestClient(options);
    }

I added to one of the tests and it works great. Thanks again @ChaseFlorell :)

How to do this with the .net core ILogger interface. Where does the output go?

@alexeyzimarev
Copy link
Member

It's the Xunit test output. HttpTraceHandler has a simple logging interface that can have any implementation.
https://github.com/BSiLabs/HttpTracer/blob/master/src/HttpTracer/Logger/ILogger.cs

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

No branches or pull requests

9 participants