Formatters and Content Negotiation
A free video tutorial from Packt Publishing
Tech Knowledge in Motion
3.9 instructor rating • 1587 courses • 308,078 students
In this video, we use formatters and content negotiation so that the client can decide in which way he wants to send and receive the data.
Learn more from the full courseRESTful Web services with ASP.NET Core
Leveraging ASP.NET Core and Visual Studio 17
03:33:54 of on-demand video • Updated August 2017
- Explore basic REST principles
- Build an ASP .NET core project using Visual Studio 2017
- Work with different environments such as development and production
- Implement a service interface to read, manipulate, and delete data
- Extend API functionality with more features
- Deploy the WebAPI
English [Auto] Welcome to the fourth section of reskill Web services with Espinal and core in this section we will take the next steps with our Espina and a core web API and this section you will learn something about core and content negotiation. We will take a look at logging and validation in our API. After that we will have a video about documentation with swagga we will implement paging sorting and filtering to our API. Also we will add versions to API and the last video we will take a look at authentication Corra API of course we will summarize in the end what we've learned so far. So let's start with the first video format is in content negotiation format and content negotiation is needed when the client wants to tell the server in which data format the data comes and which data format he accepts. So we are dealing with input matters and the output format is the input formatters are dealing with a request so the client is telling the server in which data format the data comes he can use the HTP had a Content-Type for that. The output format on the other side is dealing with the response so the client can tell the server that he is accepting the data in a specific format he can use the HGP header except for that if the content or the accept letter and not send with a request then it's OK if the API uses a default one. But if the API is not supporting a Content-Type or an except Teda the API should return the current status code for that. Let's see that in a day. So we have our API up and running in the free fire is simple get requests to the I-Slash customers. We get back at 200 OK status code and we see our results and Jason even if we take single customer here we see that we get back to resolve and Jason but why is that the reason for this is that a pyramid core as the Jasen output format are pretty evolved to tell us of what we accept as an answer. We can add and except Teda and give it a value application. Slash Jason so now we're telling the server that we as a client to accept Jason we sent this we can't beg our response bodies Jason but wonder karyotype X amount here. It's too simple and sended now we still get back a response. But as Jason and this is because ESPERA at core does not know application slash Simoneau yet and so it calls back to the default behavior which is case in this case but it would be better if we would give their current status code here and Tabeau climbed that the accepted is not supported in this case. We can do that very simple inside our API at MVC methods takes a parameter that way we can set up our API. Then we can add the behavior that the current status code should be returned when the except Teta is not supported. You can see that we're setting the property return a shoe be not acceptable to her. Let's try that the request again to our API. See we getting back Chaisson we get to see the customer can see this. We now pass the header except application Exham how we get back of forsakes. Not acceptable which is the current status code in that case we do an application Jason here. We get back a response bonniest Jason but let's add some support to our API back and what we can right click on a project and manage with packages and then browse for a package called ESPERA core MVC formatters example we insulted k we now go back tourers data file. We can simply add the same old Cometa as an output format you can see that the output from matters and nothing else in the collection and we call and on it and adding in new serialise the output from data. Let's try that out. You know first calling our customers again and we see we get back resolved let's call 1 customer. K with Chuck Norris. And now let's add the except Teda you should always add in except at is best practice Jason. We get it back as Chaisson. Now let's do it somehow. And I can see we getting back a response body as x amount. Now let's also add some support as an input formatter. So adding the input from matter is very similar to adding the output formatter. The only thing we have to do is adding an X amount Serializer input from added to the input formatters of the config. So what we're doing is we're adding a new Exham as serialize the input format to the input formatters might start the application application running. And now we can send a post request to the Euro localized API customers and resending the Content-Type. So we're saying the Content-Type of my requests body is application somehow. And we do except application Jason as a response you take a look at the body we can see that we are sending a customer create the video with a first name Charke a last name nor'west with an age of 54 as a format. We choose the application X amount and sent that. And you can see as a response we're getting back or Jason with the ID and it a one created status code in the headers. We can see that location can copy that and send a request to the euro. Here we are getting the response and Jason Acree adding except Teda application he somehow and we sended we get getting back or responds and exam. OK and this is how you can add input and output formatters to your API and giving the client the possibility to say which format he accepts and in which form at their request by he comes to the API so that the API can model binded. So let's summarize this video shot. So in this video we saw how we can add input formatters to our API. The client has to tell the server which Froment he accepts via the content type HTP header the spirit and core API has been able to bind the data from the body to the model we accept the our methods. We also saw how we can add output formatters to API with trauma response body. The client can tell that to the server where the HTP accept data in our API we are supporting Jason in the similar way Jason is the more common form and nowadays if you have an internal API Maybe you only need Jason but if you have a public API Maybe its a better idea to support them as well. We also saw how we can return to the current status code if the content is not supported. Last but not least always keep in mind to send the excerpt and the content of header which each request so that the API knows how to deal with the request you are sending in the next video. I want to take a look at logon and how we can enable logging know where API.