Serialize Web API HttpHeaders as JSON

Here’s a little C# extension class that serializes a set of HttpHeaders as a JSON string:

NOTE: It uses the JSON.NET library to do the actual serialization.

For example, if you want to serialize Web API request (or response) headers, you might do something like this:

var serializedHeaders = request.Headers.ToJson();

Or this:

var serializedHeaders = request.Headers.ToJsonIndented();

Featured Image: All rights reserved by Stephen_Bar

Leave a Reply

Your email address will not be published. Required fields are marked *