Overriding bodyParser to always assume JSON

75 views
Skip to first unread message

Brad Cavanagh

unread,
Sep 16, 2013, 5:26:53 PM9/16/13
to res...@googlegroups.com
I'd like to set up a restify server to always assume any body content sent in POST requests is JSON, thus not requiring a curl user to always add "-H 'Content-Type: application/json'". Is this possible? And, if it is possible, is it crazy?

This REST server will be internal-only and all of the content sent via POST will always be JSON, never any other type of content.

Thanks,
 Brad.

Brad Cavanagh

unread,
Sep 16, 2013, 6:39:54 PM9/16/13
to res...@googlegroups.com
I should have looked over the documentation more thoroughly, as it's covered by code that's commented "screw you client!"  :-)

Basically, in the server.pre() section, add:

req.headers['content-type'] = 'application/json';

This forces the request's Content-Type header to always be 'application/json', so when bodyParser() runs it always assumes it's JSON, and Bob's your uncle. Adding the "screw you client!" comment is optional.

Cheers,
 Brad.
Reply all
Reply to author
Forward
0 new messages