0

I am using this in some c# code in an MVC application.

var request = client.PutAsJsonAsync(path, myobj).

It works fine - but how can I snoop on the JSON that is being sent to the web server (at path)?

Is there any way to do this with the debugger - or to output the JSON that is being sent back to the MVC application? Thx.

niico
  • 11,206
  • 23
  • 78
  • 161

2 Answers2

1

You can

  • Use proxy to debug traffic
  • Use API testing tool
  • Modify your code to log requests/responses

First option - proxy. Take a look on Fiddler. It shows you request/response data and allows to see https data too. As @Balazs noted, with Fiddler you can compose, capture, edit and send requests as well.

Second option - API testing tool. There are lot of them (SoapUI, RESTClient etc), but I suggest to take a look on Postman. It allows touching your API both manually and automatically via tests.

Last option is up to you. E.g. you can create OWIN Middleware, which will log request and/or response.


UPDATE: Seems like you want to log requests on client side instead of server side. So instead of OWIN Middleware you can create custom message handler:

public class LoggingHandler : HttpClientHandler
{
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
    protected override Task<HttpResponseMessage> SendAsync(
       HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // log request or its part here
        Logger.Trace(request.ToString());
        return base.SendAsync(request, cancellationToken);
    }
}

And pass it to HttpClient:

var client = new HttpClient(new LoggingHandler());
client.DefaultRequestHeaders.Add("Accept", "application/xml");
var response = await client.GetStringAsync("http://localhost:12345/api/blah");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    It is worth noting that one can compose and/or edit requests using Fiddler as well. – Balázs Mar 13 '17 at 11:43
  • (obviously I can't use Fidler to watch C# code calling a web service directly - or if I can - how?) – niico Mar 13 '17 at 12:34
  • @niico what do you mean by *raw* request? What do you mean by *watching c# code*? It can be any code - your c# or java application, some test tool like curl or browser. Fiddler will catch request. And it's not clear on which side you want to see raw request - some client application or webapi application? – Sergey Berezovskiy Mar 13 '17 at 12:42
  • raw request - I just want to see the JSON web api is generating and sending to the web service. I am calling the web service *from C# server side code*. I want to see what is being sent. Fidler isn't catching this code - (and I'm using http://127.0.0.1). – niico Mar 13 '17 at 12:46
  • http://stackoverflow.com/questions/214308/how-do-i-get-fiddler-to-stop-ignoring-traffic-to-localhost – Sergey Berezovskiy Mar 13 '17 at 12:47
  • I am using machine name. Perhaps I'm misunderstanding but I have a request going from 'server' C# code (running on my local machine) - to a web api web service (running on my local machine). I am not using a browser to call the service directly. Should fiddler be picking this up? – niico Mar 13 '17 at 12:59
  • @niico yes, you can. If you run client service from different account, them just add defaultProxy setting to system.net in configuration file. Check also my updated code. – Sergey Berezovskiy Mar 13 '17 at 13:15
0

Create a class extending DelegatingHandler

Then ...

Protected Overrides Async Function SendAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)

    LogRequest(request) ' this is where the request is

    ' Create and process response
    Dim response As HttpResponseMessage = Nothing
    response = Await MyBase.SendAsync(request, cancellationToken)

    LogResponse(response) ' here is the reponse

    Return response
End Function

 Private Function LogResponse(response As HttpResponseMessage) As Boolean

    Try

        Dim theJSON As String = Newtonsoft.Json.JsonConvert.SerializeObject(ExtractLoggingInfoFromResponse(response))

    Catch ex As Exception
        Return False
    End Try

    Return True
End Function

....then in Global.asax, register the handler with: GlobalConfiguration.Configuration.MessageHandlers.Add(New YourClassNameHandler)

niico
  • 11,206
  • 23
  • 78
  • 161
lcryder
  • 486
  • 2
  • 8