Search code examples
visual-studiorequesthttpclient

How to see request data being sent to service in Visual Studio Debug?


How can I see the json data actually being sent in Request in Visual Studio to verify it is well formed and troubleshoot if something is wrong with data?

I have a StockItem class with example code: public class StockItem { private string _recID;

    [JsonProperty("recid")]
    public string RecID
    {
        get { return _recID; }
        set { _recID = value; }
    }
 )

That should set the name of the column being passed to web service to the one matching their column.

   public static async Task ProcessItemsAsync(object content, CancellationToken cancellationToken)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.BaseAddress = new Uri(BaseURL);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "tokencode");
            using (var request = new HttpRequestMessage(HttpMethod.Post, itemUrl))
            {
                var json = JsonConvert.SerializeObject(content);
                using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
                {
                    request.Content = stringContent;

                    using (var response = await client
                        .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                        .ConfigureAwait(false))
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
        }
   }

I look at Locals or Autos in Visual Studio...I can see my StockItem object in content [0]. But not in request.Content.

The reason in response is Bad Request. How do I troubleshoot this?

The RequestMessage is "Bad Request". Sorry...I am extremely frustrated with this.


Solution

  • You can easily see the request being sent with Fiddler. As far as I know you can't see it in Visual Studio.