Search code examples
c#asp.netasp.net-web-apirestsharp

How to get the RestRequest body content after adding file but before sending


I am calling an API that requires an MD5 hash of the body of the request in the request's header.

I am using RestSharp to send the requests. Normally I can check the Request.Parameters for the Body parameter and hash the value of the parameter before calling Execute.

Although, after calling AddFile the body parameter is empty and appears to stay empty until the content is prepared before sending the request. (Since files are stored separately)

Is there anyway to read the body content of the RestRequest after the multipart string has been generated but before the request is sent so I can add the MD5 hash to the header of the request?


Solution

  • From additional research I have done, it appears that at this time there is not a way to achieve the functionality that I was after with the default API.

    I ended up downloading the source code and added an event handler to the Http class that is now triggered before the HttpWebRequest is sent. I send the HttpWebRequest in the parameters of the event handler which is then bubbled all the way up to the RestClient.

    I can then intercept the request in the top level code and add to the headers as I please before the request is sent.

    This is probably not the most efficient modification but it works well enough for unit tests.