I have a created a few WCF REST services that accept JSON object requests and returning JSON object responses.
Public Function SaveClientInformation(info as ClientInformation) As Boolean Implements IContactInformation.SaveClientInformation
'Here info object is the de-serialized JSON that was sent
'At this point we also need access to the RAW JSON string that was sent from the Java client
dim rawJSONRequest = ?
'We take the info object and persist it out postgress database
End Function
I need to capture the raw JSON string from the Web Service method. Currently I have access to the deserialized object, but I also need the RAW JSON string that was submitted to the web service.
I have looked at the WCF documentation and looked around, unfortunately this doesn't seem to be possible. WCF does not let me access the RAW request (string) that was sent to the server.
I have tried creating a WCF Message Interceptor, however this doesn't give me access to the RAW request string, also I need this RAW string accessible from the Web Service Method.
I need access to the RAW string as well as the de-serialized JSON object from the Web Service method.
Is this possible in WCF?
I know this can be achieved using the newer ASP.NET Web API. but I cannot change our application as all of our Web Services are all built on top of WCF REST (JSON) and they are used by a number of clients.
The RAW request is required because, on the client side the Request RAW string is hashed by Java and that hash is sent in the header. Therefore the main reason for accessing this RAW request is to re-calculate the hash to check if the message was not altered during transmission.
FYI - We are using HTTPs, but we need to guarantee that the message was not meddled with, and this is the technique that has been implemented in our other Java Web Service and we need to do the exact thing in .NET WCF REST too.
You can access the request content by reading the Request.InputStream. This requires the aspNetCompatibilityEnabled="true" setting be turned on in the Web.Config.
See below:
Dim requestContent = [String].Empty
Dim requestStream = System.Web.HttpContext.Current.Request.InputStream
Dim originalPosition = requestStream.Position
Using reader = New System.IO.StreamReader(requestStream)
requestContent = reader.ReadToEnd()
End Using
requestStream.Seek(originalPosition, SeekOrigin.Begin)
Return requestContent