I need to calculate a HMAC signature of a request and then set a request header value of that signature, before sending the request of a JsonServiceClient.
I started down the track of handling the JsonSericeClient.RequestFilter, but quickly discovered that I cannot read the body form the request.GetRequestStream() in that context. (Exception: when I try to copy it stream.CopyTo()).
How can I get the body of the request from a JonServiceClient in time to set a header value?
It’s not possible to re-read a Request Stream as it’s a forward only Stream. I’ve made SerializeRequestToStream protected in this commit so you can override it in a sub class. You’ll need to write it to a MemoryStream so you can read it back to get a copy of the bytes then use it write to the Request Stream and calculate the hash.
This change is available from v4.5.7 that’s now on MyGet.
OK we now have a problem in the example you gave me.
SerializeRequestStream, line: 912 closes the passed in [MemoryStream] stream, so the example throws because the stream is closed before we can use it to calculate the hash, and then write it to the requestStream again.
I could override SerializeRequestToStream and not close the stream, but first I’d need to know why you were closing it in the first place.
Can you remember why the stream is closed in this context? I see a few lines before that new streams are used in compression. Its a little hard to decipher what the intention was here.
Perhaps an optional parameter on the method to keep the stream open (by default not)?