reading raw content from request InputStream with json vs form encoded

374 views
Skip to first unread message

Maxim Mass

unread,
Apr 19, 2012, 8:49:48 AM4/19/12
to servic...@googlegroups.com
I'm trying to access the raw request stream when the content-type is application/json or application/x-www-form-urlencoded (and the Content-Length is a reasonable size) for logging purposes. This works great for the form encoded calls but unfortunately not at all when it's json. I understand that what is likely happening is that for json requests the stream is fed into a deserializer which reads from and closes the stream. But the same must be happening as well for the form-encoded requests (that also deserialize into the same request DTO). Though I can read the input stream from one but not the other. (Yes, I'm checked if I can seek and read and moving the position to 0).

I saw from another post about implementing IRequiresRequestStream from the DTOs and not having them deserialized automatically. Is there another approach I can try to peek at the input?

Thanks!
Max


Demis Bellot

unread,
Apr 20, 2012, 12:40:08 PM4/20/12
to servic...@googlegroups.com
You should develop under the assumption that the HttpRequest InputStream is a one-time, forward only stream.
So if you want to read the stream, you need to tell ServiceStack not to which you can do with IRequiresRequestStream or registering a custom request binder, both described at:

Although if you just want to do this for logging purposes you should see if the built-in IRequestLogger is useful:

It is enabled in the SocialBootstrapApi project at:

Which by default is view-able at /api/requestlogs for any users in the Admin role (both configurable). 

In SocialBootstrapApi anytime a UserName/Email is registered that exists in AdminUserNames Web.config:
It is automatically assigned to the Admin role:

Cheers,

Reply all
Reply to author
Forward
0 new messages