86

I'm using RestSharp to make calls to a webservice. All is well but I was wondering if it would be possible to print the raw request headers and body that is sent out and the raw response headers and the response body that comes back.

This is my code where I create a request and get a response back

public static TResponse ExecutePostCall<TResponse, TRequest>(String url, TRequest requestData, string token= "") where TResponse : new()
{
    RestRequest request = new RestRequest(url, Method.POST);
    if (!string.IsNullOrWhiteSpace(token))
    {
        request.AddHeader("TOKEN", token);
    }


    request.RequestFormat = DataFormat.Json;
    request.AddBody(requestData);

    // print raw request here

    var response = _restClient.Execute<TResponse>(request);

    // print raw response here

    return response.Data;
}

so, Would it be possible to print the raw request and response?

Professor Chaos
  • 8,850
  • 8
  • 38
  • 54
  • 3
    do you want to do this every time or just to debug something? if just a one-off then use fiddler to get the raw requests going back and forth – wal Mar 28 '13 at 14:04
  • Not a complete answer, but you can write your own serializer/deserializer and log the genereated/consumed JSON there. But you might be better off with a "sniffing" proxy as suggested above. – NilsH Mar 28 '13 at 14:06
  • @wal I have been using fiddler. I want to do this everytime in my .net app. – Professor Chaos Mar 28 '13 at 14:10
  • do u need the body or just the headers? 'everything' seems a bit overkill but i dont know what it is you want exactly. – wal Mar 28 '13 at 14:14
  • @wal I definitely want the HTTP response code and the response body.. for request, I need the method and the url and the request body. – Professor Chaos Mar 28 '13 at 14:19

10 Answers10

93

RestSharp doesn't provide a mechanism to achieve exactly what you want and activating the .Net tracing is a bit overkilling IMO.

For logging (debugging) purposes (something that I can leave turned on for a while in PROD for example) I have found this approach to be very useful (although it has some details on how to call it, read below the code):

private void LogRequest(IRestRequest request, IRestResponse response, long durationMs)
{
        var requestToLog = new
        {
            resource = request.Resource,
            // Parameters are custom anonymous objects in order to have the parameter type as a nice string
            // otherwise it will just show the enum value
            parameters = request.Parameters.Select(parameter => new
            {
                name = parameter.Name,
                value = parameter.Value,
                type = parameter.Type.ToString()
            }),
            // ToString() here to have the method as a nice string otherwise it will just show the enum value
            method = request.Method.ToString(),
            // This will generate the actual Uri used in the request
            uri = _restClient.BuildUri(request),
        };

        var responseToLog = new
        {
            statusCode = response.StatusCode,
            content = response.Content,
            headers = response.Headers,
            // The Uri that actually responded (could be different from the requestUri if a redirection occurred)
            responseUri = response.ResponseUri,
            errorMessage = response.ErrorMessage,
        };

        Trace.Write(string.Format("Request completed in {0} ms, Request: {1}, Response: {2}",
                durationMs, 
                JsonConvert.SerializeObject(requestToLog),
                JsonConvert.SerializeObject(responseToLog)));
}

Things to note:

  • Headers, Url segments, QueryString parameters, body, etc. all are considered parameters for RestSharp, all that appear in the parameters collection of the request, with their corresponding type.
  • The log method must be called AFTER the request took place. This is needed because of the way RestSharp works, the Execute method will add headers, run the authenticators (if some configured), etc. and all these will modify the request. So in order to have the all the real parameters sent logged, the Execute method should have been called before logging the request.
  • RestSharp itself will never throw (instead errors get saved in the response.ErrorException property), but I think deserialization could throw (not sure) and besides I needed to log the raw response, so I chose to implement my own deserialization.
  • Have in mind that RestSharp uses its own formatting when converting parameters values to generate the Uri, so serializing the parameters to log them may not show the exact same things that were put in the Uri. That's why the IRestClient.BuildUri method it's pretty cool to get the actually called Uri (including the base url, the replaced url segments, the added queryString parameters, etc).
  • EDIT: Also have in mind that it could happen that the serializer RestSharp is using for the body is not the same this code is using, so I guess code could be adjusted to use request.JsonSerializer.Serialize() for rendering the body parameter (I haven't tried this).
  • Some custom code was needed to achieve nice descriptions in the logs for the enums values.
  • StopWatch usage could be moved around to include deserialization in the measuring.

Here it is a basic complete base class example with logging (using NLog):

using System;
using System.Diagnostics;
using System.Linq;
using NLog;
using Newtonsoft.Json;
using RestSharp;

namespace Apis
{
    public abstract class RestApiBase
    {
        protected readonly IRestClient _restClient;
        protected readonly ILogger _logger;

        protected RestApiBase(IRestClient restClient, ILogger logger)
        {
            _restClient = restClient;
            _logger = logger;
        }

        protected virtual IRestResponse Execute(IRestRequest request)
        {
            IRestResponse response = null;
            var stopWatch = new Stopwatch();

            try
            {
                stopWatch.Start();
                response = _restClient.Execute(request);
                stopWatch.Stop();

                // CUSTOM CODE: Do more stuff here if you need to...

                return response;
            }
            catch (Exception e)
            {
                // Handle exceptions in your CUSTOM CODE (restSharp will never throw itself)
            }
            finally
            {
                LogRequest(request, response, stopWatch.ElapsedMilliseconds);
            }

            return null;
        }

        protected virtual T Execute<T>(IRestRequest request) where T : new()
        {
            IRestResponse response = null;
            var stopWatch = new Stopwatch();

            try
            {
                stopWatch.Start();
                response = _restClient.Execute(request);
                stopWatch.Stop();

                // CUSTOM CODE: Do more stuff here if you need to...

                // We can't use RestSharp deserialization because it could throw, and we need a clean response
                // We need to implement our own deserialization.
                var returnType = JsonConvert.DeserializeObject<T>(response.Content);
                return returnType;
            }
            catch (Exception e)
            {
                // Handle exceptions in your CUSTOM CODE (restSharp will never throw itself)
                // Handle exceptions in deserialization
            }
            finally
            {
                LogRequest(request, response, stopWatch.ElapsedMilliseconds);
            }

            return default(T);
        }

        private void LogRequest(IRestRequest request, IRestResponse response, long durationMs)
        {
            _logger.Trace(() =>
            {
                var requestToLog = new
                {
                    resource = request.Resource,
                    // Parameters are custom anonymous objects in order to have the parameter type as a nice string
                    // otherwise it will just show the enum value
                    parameters = request.Parameters.Select(parameter => new
                    {
                        name = parameter.Name,
                        value = parameter.Value,
                        type = parameter.Type.ToString()
                    }),
                    // ToString() here to have the method as a nice string otherwise it will just show the enum value
                    method = request.Method.ToString(),
                    // This will generate the actual Uri used in the request
                    uri = _restClient.BuildUri(request),
                };

                var responseToLog = new
                {
                    statusCode = response.StatusCode,
                    content = response.Content,
                    headers = response.Headers,
                    // The Uri that actually responded (could be different from the requestUri if a redirection occurred)
                    responseUri = response.ResponseUri,
                    errorMessage = response.ErrorMessage,
                };

                return string.Format("Request completed in {0} ms, Request: {1}, Response: {2}",
                    durationMs, JsonConvert.SerializeObject(requestToLog),
                    JsonConvert.SerializeObject(responseToLog));
            });
        }
    }
}

This class will log something like this (pretty formatted for pasting here):

Request completed in 372 ms, Request : {
    "resource" : "/Event/Create/{hostId}/{startTime}",
    "parameters" : [{
            "name" : "hostId",
            "value" : "116644",
            "type" : "UrlSegment"
        }, {
            "name" : "startTime",
            "value" : "2016-05-18T19:48:58.9744911Z",
            "type" : "UrlSegment"
        }, {
            "name" : "application/json",
            "value" : "{\"durationMinutes\":720,\"seats\":100,\"title\":\"Hello StackOverflow!\"}",
            "type" : "RequestBody"
        }, {
            "name" : "api_key",
            "value" : "123456",
            "type" : "QueryString"
        }, {
            "name" : "Accept",
            "value" : "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml",
            "type" : "HttpHeader"
        }
    ],
    "method" : "POST",
    "uri" : "http://127.0.0.1:8000/Event/Create/116644/2016-05-18T19%3A48%3A58.9744911Z?api_key=123456"
}, Response : {
    "statusCode" : 200,
    "content" : "{\"eventId\":2000045,\"hostId\":116644,\"scheduledLength\":720,\"seatsReserved\":100,\"startTime\":\"2016-05-18T19:48:58.973Z\"",
    "headers" : [{
            "Name" : "Access-Control-Allow-Origin",
            "Value" : "*",
            "Type" : 3
        }, {
            "Name" : "Access-Control-Allow-Methods",
            "Value" : "POST, GET, OPTIONS, PUT, DELETE, HEAD",
            "Type" : 3
        }, {
            "Name" : "Access-Control-Allow-Headers",
            "Value" : "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept",
            "Type" : 3
        }, {
            "Name" : "Access-Control-Max-Age",
            "Value" : "1728000",
            "Type" : 3
        }, {
            "Name" : "Content-Length",
            "Value" : "1001",
            "Type" : 3
        }, {
            "Name" : "Content-Type",
            "Value" : "application/json",
            "Type" : 3
        }, {
            "Name" : "Date",
            "Value" : "Wed, 18 May 2016 17:44:16 GMT",
            "Type" : 3
        }
    ],
    "responseUri" : "http://127.0.0.1:8000/Event/Create/116644/2016-05-18T19%3A48%3A58.9744911Z?api_key=123456",
    "errorMessage" : null
}

Hope you find this useful!

Mark Whitaker
  • 8,465
  • 8
  • 44
  • 68
LucasMetal
  • 1,323
  • 10
  • 16
  • 2
    very useful and compact – Konstantin Chernov Jul 28 '16 at 11:28
  • I modified your code to be a decorator to the IRestClient interface instead of a base class, and use log4net instead of NLog. log4net needs a bit more help at writing out arrays of anonymous types, but otherwise it works great. – David Keaveny Sep 12 '16 at 04:59
  • @DavidKeaveny awesome! I'm not familiarized with adding decorators to the IRestClient interface, what class finally uses it? Do you have any link to check the implementation? Also check that I added an extra bullet point ;) Thanks for commenting. – LucasMetal Sep 12 '16 at 12:20
  • 2
    Thank You. Works great! @LucasG.Devescovi could You share Your decorator code? – Misiu Oct 04 '17 at 10:06
  • I'm glad it worked for you! Regarding the decorator code, it was @DavidKeaveny the one who did it, I don't have it. Thanks ;) – LucasMetal Oct 10 '17 at 01:21
  • Does the Execute method will never throw? So can I safely log (request/response) after it, without any try catch surround? Thanks – Andre Dec 09 '18 at 12:54
  • Hi @Andre, according to the recommended usage in the RestSharp wiki, the Execute method will never throw, take a look at the text before the first example here: https://github.com/restsharp/RestSharp/wiki/Recommended-Usage . Anyway, the custom code that you put around it could throw, so depending on your use case you maybe want to try/catch it, take a look at my example class. Hope this helps ;) – LucasMetal Dec 10 '18 at 13:51
  • 2
    There is a Nuget package for automatic logging of RestSharp requests and responses to Serilog: https://www.nuget.org/packages/RestSharp.Serilog.Auto/ – yallie Apr 17 '20 at 11:35
  • RestSharp.Serilog.Auto is extremely heavy on the dependencies. I would not recommend it to anyone trying keep their project sizes down. – tno2007 Dec 14 '20 at 13:11
  • Restsharp does support this now using the `RestRequest.OnBeforeRequest` event – Andrew Richesson Mar 21 '23 at 19:44
31

.net provides its own yet powerful logging feature. This can be turned on via config file.

I found this tip here. John Sheehan pointed to How to: Configure Network Tracing article. (a note: I edited the config provided, turned off unnecessary (for me) low level logging).

  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="protocolonly" maxdatasize="1024">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Http">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
      <add name="System.Net.Http" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.WebSockets" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
akava
  • 542
  • 7
  • 14
8

You have to loop through the request.Parameters list and format it to a string in whatever format you like.

var sb = new StringBuilder();
foreach(var param in request.Parameters)
{
    sb.AppendFormat("{0}: {1}\r\n", param.Name, param.Value);
}
return sb.ToString();

If you want the output to show request headers and then the body similar to Fiddler, you just need to order the collection by Request headers and then by Request body. The Parameter object in the collection has a Type parameter enum.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
7

I just found the code below in the RestSharp examples. It allows you to print your raw response.

client.ExecuteAsync(request, response =>
                   {
                       Console.WriteLine(response.Content);
                   });
Leigh
  • 28,765
  • 10
  • 55
  • 103
Jiongfeng Luo
  • 141
  • 1
  • 7
3

You can use Fiddler for capturing HTTP requests.

Neshta
  • 2,605
  • 2
  • 27
  • 45
  • 1
    Sometimes it's nice to be able to log these types of things when trying to troubleshoot an issue that a client is having without installing Fiddler. – The Muffin Man Jan 18 '16 at 23:30
  • 1
    How do we use fiddler here, do we need to change something in the config? – blogbydev Sep 22 '16 at 06:50
  • @singsuyash Sorry, but I don't remember why I answered so year ago. I've read the question again and now I think that it is not a good answer. But I see that I voted this asnwer http://stackoverflow.com/a/21989958/1277458 – Neshta Sep 23 '16 at 09:20
  • Fiddler works, but sometimes you have to tweak the settings (ex. decrypt traffic) – live-love Aug 01 '19 at 17:11
3

I know this post is almost 10 years old, sorry about that. You can now specify delegates just before sending the request, and just after receiving the response.

This is how I've implemented this particular need.

public class TraceRequest : RestRequest
{

    #region Properties

    private bool TraceStreamContent { get; set; }

    #endregion

    #region Constructor

    public TraceRequest(string pResource, bool pTraceStreamContent)
        : base(pResource)
    {
        this.TraceStreamContent = pTraceStreamContent;
        this.InitializeLogs();
    }

    #endregion

    #region Methods

    private void InitializeLogs()
    {
        this.OnBeforeRequest = this.OnBeforeRequestMethod;
        this.OnAfterRequest = this.OnAfterRequestMethod;
    }

    private ValueTask OnBeforeRequestMethod(HttpRequestMessage pMessage)
    {
        var builder = new StringBuilder();

        builder.AppendLine("------------------------------");
        builder.AppendLine("REQUEST [{0}] {1}", pMessage.Method, pMessage.RequestUri);

        foreach (var header in pMessage.Headers)
        {
            builder.AppendLine("  {0}: {1}", header.Key, string.Join(';', header.Value));
        }

        if (this.TraceStreamContent)
        {
            var stream = pMessage.Content.ReadAsStream();

            this.ReadStream(stream, builder);
        }
        else
        {
            this.ReadContent(pMessage.Content, builder);
        }

        builder.AppendLine("------------------------------");

        var content = builder.ToString();

        Console.WriteLine(content);

        return ValueTask.CompletedTask;
    }

    private void ReadContent(HttpContent pContent, StringBuilder pBuilder)
    {
        foreach (var header in pContent.Headers)
        {
            pBuilder.AppendLine("  {0}: {1}", header.Key, string.Join(';', header.Value));
        }

        this.ReadContent(pContent as StreamContent, pBuilder);
        this.ReadContent(pContent as StringContent, pBuilder);
        this.ReadContent(pContent as MultipartFormDataContent, pBuilder);

        Console.WriteLine();
    }

    private void ReadContent(MultipartFormDataContent pContent, StringBuilder pBuilder)
    {
        if (pContent != null)
        {
            foreach (var content in pContent)
            {
                pBuilder.AppendLine();
                this.ReadContent(content, pBuilder);
            }
        }
    }

    private void ReadContent(StreamContent pContent, StringBuilder pBuilder)
    {
        if (pContent != null)
        {
            var stream = pContent.ReadAsStream();
            pBuilder.AppendLine("  contains {0} bytes", stream.Length);
        }
    }

    private void ReadContent(StringContent pContent, StringBuilder pBuilder)
    {
        if (pContent != null)
        {
            var stream = pContent.ReadAsStream();
            pBuilder.Append("  ");
            this.ReadStream(stream, pBuilder);
        }
    }

    private void ReadStream(Stream pStream, StringBuilder pBuilder)
    {
        var index = 0L;
        var length = pStream.Length;
        var buffer = new byte[1024];

        while (index < length - 1)
        {
            var read = pStream.Read(buffer, 0, 1024);
            var result = Encoding.UTF8.GetString(buffer, 0, read);

            pBuilder.Append(result);

            index += read;
        }

        pBuilder.AppendLine();

        pStream.Seek(0L, SeekOrigin.Begin);
    }

    private ValueTask OnAfterRequestMethod(HttpResponseMessage pMessage)
    {
        var builder = new StringBuilder();

        builder.AppendLine("------------------------------");
        builder.AppendLine("RESPONSE {2} [{0}] {1}", pMessage.RequestMessage.Method, pMessage.RequestMessage.RequestUri, pMessage.StatusCode);

        foreach (var header in pMessage.Headers)
        {
            builder.AppendLine("  {0}: {1}", header.Key, string.Join(';', header.Value));
        }

        if (this.TraceStreamContent)
        {
            var stream = pMessage.Content.ReadAsStream();

            this.ReadStream(stream, builder);
        }
        else
        {
            this.ReadContent(pMessage.Content, builder);
        }

        builder.AppendLine("------------------------------");

        var content = builder.ToString();

        Console.WriteLine(content);

        return ValueTask.CompletedTask;
    }

    #endregion
}

You can now use the class TraceRequest like this, the boolean TraceStreamContent will enable a complete HTTP tracing. Might be too big when sending files in attachment.

        var client = new RestClient("https://dev.test.cloud:4511")
        {
            Authenticator = new HttpBasicAuthenticator("user", "password")
        };
        var request = new TraceRequest("test", true)
            .AddJsonBody(fax)
            .AddFile("first", new byte[] { 65, 66, 67, 68 }, "first.txt")
            .AddFile("second", new byte[] { 69, 70, 71, 72 }, "second.txt");

        var response = client.Post(request);

Hope this will help someone !

EDIT

Here is the missing extension method for StringBuilder

    public static void AppendLine(this StringBuilder pBuilder, string pFormat, params object[] pArgs)
    {
        pBuilder.AppendFormat(pFormat, pArgs);
        pBuilder.AppendLine();
    }
Mad hatter
  • 569
  • 2
  • 11
1

As a partial solution, you can use RestClient's BuildUri method:

var response = client.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
    throw new Exception($"Failed to send request: {client.BuildUri(request)}");
starteleport
  • 1,231
  • 2
  • 11
  • 21
0

An option is use your own authenticator. RestSharp allows to inject an authenticator:

var client = new RestClient();
client.Authenticator = new YourAuthenticator(); // implements IAuthenticator

public interface IAuthenticator
{
    void Authenticate(IRestClient client, IRestRequest request);
}

internal class YourAuthenticator: IAuthenticator
{
  public void Authenticate(IRestClient client, IRestRequest request)
  {
    // log request
  }
}

The authenticator’s Authenticate method is the very first thing called upon calling RestClient.Execute or RestClient.Execute. The Authenticate method is passed the RestRequest currently being executed giving you access to every part of the request data (headers, parameters, etc.) from RestSharp's wiki

This means than in the Authenticate method you can log the request.

giganoide
  • 19
  • 1
  • 5
  • 1
    I need to get the raw request value, exactly as sent, so I would like to know how implement the authenticator will help me, could you help? thanks – Andre Dec 09 '18 at 12:50
  • @Andre I added how you can implement the authenticator. Usually log the header and the body it's enough, otherwise it's still possible to access all the parameters of the request. – giganoide Dec 10 '18 at 09:07
0

If you just want to take a look at the raw response, try overriding the deserialiser (most of this copy-pasted from restsharp 107.3.0):

using RestSharp.Serializers;
// …
public class StupidLogSerialiser  : IRestSerializer, ISerializer, IDeserializer {
    public string Serialize(object obj) => null;
    public string Serialize(Parameter bodyParameter) => Serialize(null);
    public T Deserialize<T>(RestResponse response) {
        Console.WriteLine(response.Content);
        return default(T);
    }
    public string ContentType { get; set; } = "application/json";
    public ISerializer         Serializer           => this;
    public IDeserializer       Deserializer         => this;
    public DataFormat          DataFormat           => DataFormat.Json;
    public string[]            AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept;
    public SupportsContentType SupportsContentType  => contentType => contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase);
}
// …
client.UseSerializer(() => new StupidLogSerialiser());
unhammer
  • 4,306
  • 2
  • 39
  • 52
-3

You can try to use

Trace.WriteLine(request.JsonSerializer.Serialize(request));

to get request and

response.Content(); // as Luo have suggested

request is not the same, as Fiddler shows but it contains all data and is readable (with some RestSharp garbage at the end).

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • 2
    `Trace.WriteLine(request.JsonSerializer.Serialize(request))` will not give you the **raw** request, all it will do is serialize the object, which is a completely different thing. – Alex.F Aug 03 '14 at 07:18