Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's see how Web API uses content negotiation to inspect the incoming request and determine the "best" data format to use when serializing our data.
Follow Along
To follow along committing your changes to this course, you'll need to fork the aspnet-fitness-frog-spa repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-fitness-frog-spa
git checkout tags/v2.4 -b understanding-content-negotiation
Additional Learning
When we make a GET request against
our entry's endpoint using Postman,
0:00
we can see that the response
contains JSON formatted data.
0:04
While there are no restrictions on the
format of data that Rest APIs return in
0:09
their responses,
the majority of APIs return JSON, or
0:12
JavaScript object notation formatted data.
0:17
JSON, it's easy for
humans to read and write.
0:21
And it's easy for
machines to parse and generate.
0:24
This opening bracket,
which is closed all the way down here,
0:31
Indicates a list or array of values.
0:37
A set of curly braces indicates
a collection of name value pairs.
0:46
Which is commonly
referred to as an object.
0:51
Each object property has
a name in double quotes,
0:54
Followed by a colon and
then the property value.
1:00
A property value can be a string in
double quotes or as you see here for
1:05
the ID property, a number.
1:09
Or it could even be true or false or null.
1:12
Most if not all modern languages
provide support for working with JSON.
1:17
And as you might expect, given that the
name of the data format contains the word
1:22
JavaScript, it's very easy to work with
JSON when writing JavaScript code.
1:26
For more information on JSON,
see the teachers notes.
1:31
We're returning the collection of
entry objects from our Get method.
1:40
So how was our collection
being serialized to JSON?
1:44
What we're seeing in action is
a feature called, content negotiation.
1:48
By returning a collection of objects or
a single object from an action method,
1:52
we're allowing Web API to
inspect the incoming request and
1:58
determine the best data format to
use when serializing our data.
2:02
In Postman, we can add an accept
header to our request.
2:07
And set its value to a media type
representing our preferred data format.
2:11
Media types are two part identifiers for
file, content or
2:21
data formats that
are used on the Internet.
2:25
And media type consists of a top-level
type name and a Sub-Type name.
2:29
Common Top-Level names
include application, audio,
2:34
font, image, text, and video.
2:39
Though, when working with services, you'll
most often use application and text.
2:43
Sub-Type names typically
consist of a Media Type name.
2:47
Common rest API media types include json,
xml, and
2:52
x-www-form-urlencoded.
2:56
Putting it all together,
you end up with application/json,
3:00
application/xml, and
application/x-www-form-urlencoded.
3:06
If we send an accept header
value of application/json,
3:14
we'll receive JSON-formatted
data in the response body.
3:18
If we send a value of application/XML,
3:31
We'll receive XML formatted data.
3:38
To serialize our data to
the client's preferred data format,
3:46
Web API uses a media type formatter.
3:50
Media type formatters are used to not
only serialize outgoing data to response
3:53
message bodies, but also to de-serialize
incoming data from request message bodies.
3:58
Web API's built-in media type
formatters provides support for
4:04
JSON, XML, BSON, and
form URL-encoded data.
4:09
You can also create your own
custom media type formatters.
4:14
For more information on how to do that,
see the teacher's notes.
4:18
Web APIs built-in JSON media type
formatter utilizes the popular
4:24
Json.NET framework.
4:29
In the next section, we'll see how we
can configure the Json.NET serializer
4:31
to refine the results
returned to the client.
4:36
By delegating the responsibility
of serializing our data or
4:39
allowing Web API to enable the client
4:43
to specify the response message
body data format that they prefer.
4:45
This is the power of content negotiation.
4:50
Now that we've taken a closer look at
how our data flows from controller
4:52
action methods to responses.
4:57
In the next video let's look
at the reverse scenario,
4:59
how data flows from requests to
our controller action methods.
5:02
You need to sign up for Treehouse in order to download course files.
Sign up