Running Rabbit
Running Rabbit

Reputation: 2720

How to get input parameters from HttpRequestMessage

I have created a class that inherit from DelegatingHandler and overwrite the SendAsync method. I wanted to validate the request parameters and encode them with AntiXss class before passing it to the main controller. Therefore, I created this. Now when I call the respective controller via SoapUI, I successfully get into the Async method and gets the request object.

Difficulty I am not able to fetch the request parameters from the HTTPREQUESTMESSAGE object that I passed from the soap ui. Below is the snapshot of the request

enter image description here

CODE

protected override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        //Encode here the request object 
        var requestObj= request.GetQueryNameValuePairs()
                   .ToDictionary(kv => kv.Key, kv => kv.Value,
                        StringComparer.OrdinalIgnoreCase);
        // work on the request
        System.Diagnostics.Trace.WriteLine(request.RequestUri.ToString());

        return base.SendAsync(requestObj, cancellationToken)
         .ContinueWith(task =>
         {
           // work on the response
           var response = task.Result;
             response.Headers.Add("X-Dummy-Header", Guid.NewGuid().ToString());
             return response;
         });
    }

    #endregion
}

I just wanted to get the values of the parameters that I passed from the SOAP UI under the object of the HTTPRequestMessage. But not succeeded till now. Any help is appreciated.

Upvotes: 1

Views: 6179

Answers (1)

Running Rabbit
Running Rabbit

Reputation: 2720

After going through certain articles and question I finally got the solution:

var content = request.Content;
        string jsonContent = content.ReadAsStringAsync().Result;

The above code worked perfectly

Upvotes: 2

Related Questions