Question: Get response data from SendAsync method web API Message Handlers


Get response data from SendAsync method web API Message Handlers

Answers 1
Added at 2017-01-02 13:01

I want to fetch returned data from Web api and save in my database.

My Message handler code is here:

protected override async Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken cancellationToken)
    Stopwatch watch = new Stopwatch();
    TimeSpan second;
    // Call the inner handler.
    var response = await base.SendAsync(request, cancellationToken);
    second = watch.Elapsed;
    if (second.Seconds > 5)
            var req = JsonConvert.SerializeObject(request.GetRouteData());
            var data = JsonConvert.SerializeObject(response.Content);

            var container = UnityConfig.GetConfiguredContainer();
            IPerformanceBal performance = container.Resolve<PerformanceBal>();
            performance.SavePerformance(new ApiPerformance()
                CreatedAt = DateTime.Now,
                ExecutionTime = second,
                QueryResult = data,
                Uri = request.RequestUri.AbsoluteUri
        catch (Exception exception)


    return response;

I want actually to get data in "data" variable returned from response... somethig like ""... Any Help on thhis??

nr: #1 dodano: 2017-01-03 05:01

You can access the response content directly using the HttpContent.ReadAsStringAsync Method method like this...

//...other code

var responseContent = await response.Content.ReadAsStringAsync();

//...other code

From there you should be able to use it as needed.

Source Show
◀ Wstecz