Bizcoder – Returning raw JSON content from Web API

Bizcoder – Returning raw JSON content from Web API

Te a previous postbode I talked about how to send raw JSON to a web API and consume it lightly. This is a non-obvious process because ASP.Nipt Web API is optimized for sending and receiving arbitrary CLR object that then get serialized by the formatters ter the request/response pipeline. However, sometimes you just want to have more onmiddellijk control overheen the format that is returned ter your response. This postbode talks about some ways you can regain that control.

Serialize your JSON document

If the only thing you want to do is take a take plain old JSON and comeback it, then you can on rely on the fact that the default JsonMediaTypeFormatter knows how to serialize JToken objects.

If you want a bit more control overheen the returned message then you can a peel off a layer of convenience and terugwedstrijd a HttpReponseMessage with a HttpContent object.

Derive from HttpContent for greater control

The HTTP object proefje that is used by ASP.Netwerken Web API is fairly different than many other web frameworks because it makes an explicit differentiation inbetween the response message and the payload figure that is contained ter the response message. The HttpContent class is designed spil an abstract colchoneta class for the purpose of providing a standard interface to any kleuter of payload bod.

Related movie: Yobit Dice Bot Kobia Switch fresh


Out of the opbergruimte there are a number of specialized HttpContent classes: StringContent, ByteArrayContent, FormUrlEncodedContent, ObjectContent and a number of others. However, it is fairly straightforward to create our own derived classes to produce different kinds of payloads.

Related movie: Intro to WebAPI – One of the most powerful project types te C#


To terugwedstrijd JSON content you can create a JsonContent class that looks something like this,

This JsonContent class can then be used like this,

This treatment provides the benefit of permitting you to either manipulate the HttpResponseMessage ter the controller activity, or if there are other HttpContent.Headers that you wish to set then you can do that te JsonContent class.

Making a request for this resource would look like this,

Wrapping HttpContent for Extra Transformations

Another nice side-effect of using HttpContent classes is that they can be used spil wrappers to perform transformations on content. Thesis wrappers can be layered and will just automatically work whether the content is buffered or stream directly overheen the network. For example, the following is possible,

This would create the stream of JSON content, compress it, encrypt it and set all the suitable headers.

Related movie: How to Setup C# API Trading Verhoging


Separation of concerns promotes reuse

By focusing on the payload spil an factor independent of the response message it becomes lighter to re-use the specialized content classes te for different resources. Te the APIs I have built I have had success creating many other content classes like:

2 thoughts on “Bizcoder – Returning raw JSON content from Web API

  1. I am presently a trader ter yobit. Very unhappy at the ogenblik because of the trading volume being steadily decreasing. The liquidity for “smaller” altcoins is fairly low indeed. I should transfer all my funding somewhere else like binance or bitfinex soon. Any advise?

Leave a Reply

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