Refit: How to debug

Created on 2 Feb 2015  路  2Comments  路  Source: reactiveui/refit

Hi Paul,

Could you us some guidance on how to debug when an api call doesn't return the expected result? For example, when I do an api call, I get back null instead of a User object. When I drill into the methods being executed, I would like to end up somewhere so that I can see the response (content) to get some indication of why I got null.

Would it be nice to add some debugging lines for each request/response? Like status, headers, content, etc

outdated question

Most helpful comment

I've created this logging mechanism myself...

Don't know it this is the way to go, but I can now get some logging going...

public class LoggingMessageHandler : DelegatingHandler
    {
        public LoggingMessageHandler(HttpMessageHandler innerHandler)
            : base(innerHandler)
        {
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Debug.WriteLine("Request:");
            Debug.WriteLine(request.ToString());
            if (request.Content != null)
            {
                Debug.WriteLine(await request.Content.ReadAsStringAsync());
            }

            var response = await base.SendAsync(request, cancellationToken);

            Debug.WriteLine("Response:");
            Debug.WriteLine(response.ToString());
            if (response.Content != null)
            {
                Debug.WriteLine(await response.Content.ReadAsStringAsync());
            }

            return response;
        }
    }

All 2 comments

I've created this logging mechanism myself...

Don't know it this is the way to go, but I can now get some logging going...

public class LoggingMessageHandler : DelegatingHandler
    {
        public LoggingMessageHandler(HttpMessageHandler innerHandler)
            : base(innerHandler)
        {
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Debug.WriteLine("Request:");
            Debug.WriteLine(request.ToString());
            if (request.Content != null)
            {
                Debug.WriteLine(await request.Content.ReadAsStringAsync());
            }

            var response = await base.SendAsync(request, cancellationToken);

            Debug.WriteLine("Response:");
            Debug.WriteLine(response.ToString());
            if (response.Content != null)
            {
                Debug.WriteLine(await response.Content.ReadAsStringAsync());
            }

            return response;
        }
    }

@promontis I would start by setting up something like Runscope to see what the response content is, and see if that deserializes to the object you want (you can also change the return type to string to just see it as text)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahinyyurt picture sahinyyurt  路  5Comments

ahmedalejo picture ahmedalejo  路  6Comments

ColKrumpler picture ColKrumpler  路  3Comments

fonix232 picture fonix232  路  4Comments

jstafford5380 picture jstafford5380  路  4Comments