RESTful API Design + Call API Using HttpClient In ASP.NET

Tram Ho

Introduce

RESTful API is a standard used in designing APIs for software, applications and web services to facilitate the management of resources. System resources such as text files, photos, videos, audio or mobile data are the target it is aiming for, including resource states that are formatted and transmitted over HTTP.

  • Or to put it another way, it would be a civet to cross if the web pages agreed on returning the respective data. But hundreds of people cannot see it with their own eyes, so in this article I will not go into the theoretical depth, just give a general overview of how RESTful works => from there give a demo to be able to observe better.

I/ Restful API overview

1. Restful API Request Forms

Http Method includes 9 types but RESTful uses only 4 popular types

  • GET (SELECT): Returns a Resource or Resource list.
  • POST (CREATE): Create a new Resource.
  • PUT (UPDATE): Update information for Resource.
  • DELETE (DELETE): Delete a Resource.
    • => Corresponding to the name commonly known as CRUD (Create, Read, Update, Delete)

2. Restful Design Principles

When we send a request to a certain API, there will be some status codes to recognize as follows:

  • 200 OK – Return success for all methods
  • 201 Created – Returns when a Resource has been successfully created.
  • 204 No Content – Returns when the Resource is successfully deleted.
  • 304 Not Modified – Client can use the cache data.
  • 400 Bad Request – Invalid request
  • 401 Unauthorized – Request requires auth.
  • 403 Forbidden – denied without permission.
  • 404 Not Found – Cannot find resource from URL.
  • 405 Method Not Allowed – Method disallowed for current user.
  • 410 Gone – The resource is no longer available, the old version is no longer supported.
  • 415 Unsupported Media Type – This Resource type is not supported.
  • 422 Unprocessable Entity – Data is not validated.
  • 429 Too Many Requests – Request denied due to restriction.
  • The API is designed to be clear, you must know what the API does by looking at it

3. Advantages

  • Make the application clearer.
  • The REST URL represents the resource, not the action.
  • Data is returned with many different formats such as: xml, html, rss, json …
  • The code is simple and concise.
  • REST focuses on system resources.

II/ DEMO Simple Restful API design

I will create a Web API project and demo to try the way Restful is called

The idea is that I will create a Model class to handle in the API part and then be called by the Controller to show the View (if you are still confused about this part, everyone should preview the MVC model)


1. Section SETUP

I will create a class called Users in the Folder Model to use during the test of the API !!

  • Class Users will look like this

Create Controller part of WebAPI

  • Create a Controller Web API => I will name it UserController

 

  • Instead of using a database to easily stored and interacting with data BUT because I am lazy to create a database and connect data types, I will add directly in the code for a quicker !!
  • Add 1 function AllUser to create a User List to test

  • We will reinstall Uri of the API by going to Folder AppStart/WebApiConfig.cs and fix the routeTemplate was“api/{controller}/{action}/{id}”
  • Add the Json Formatter code below to format the returned data as json.

2. API creation section

  • Return execution is HttpResponseMessage to determine StatusCode How much is returned (return execution StatusCode as part Design principles RESTful which I mentioned in part 2)
  • Or can return with Json error inside Content of HttpClient

2.1. GET METHOD

  • PART USERCONTROLLER
    • Add the code that implements the GET method to get all Users

  • The website’s url will be the part host:port + /api/{tên controller}/{action của controller}/{tham số nếu có} as configured in the Config file.
  • When accessing the address /api/User/GetAllUser We will get 5 User created as Json

2.2 POST METHOD

In the Post, Put and Delete sections, later, you will have to use a support tool to send a request to determine what the data is obtained, because the request sent will be in the form of an invisible send.

  • PART USERCONTROLLER
    • Add the code used to POST upload data
    • This function is used to create 1 more user and return the list of users that have been added to the Content section of HttpresponseMessage.

  • POST SEND POST REQUEST
    • Post can be sent in json or urlencoded format
    • The returned data has added the user le ngoc son to the list

To be able to customize to only receive outgoing Request in Uri or just Body we can modify the function’s parameter.

  • public HttpResponseMessage CreateNew([FromBody] Users u) // Get parameters on the Body side sent up.
  • public HttpResponseMessage CreateNew([FromUri] Users u) // Get the parameter on the Url side

2.3. PUT METHOD

Similar to Post, we will add a function to Update user information

  • PART USERCONTROLLER
    • One more function used to update user information using Method is PUT

  • SEND PUT REQUEST
    • Update information of user 1 by entering the correct username to update, the update data will be the rest of the information.
    • The data returns user 1 with updated password, fullname and inactive

2.3. DELETE METHOD

  • PART USERCONTROLLER
  • Add the DeleteUser function by passing User and comparing username (usually update or delete will use a separate Id attribute to determine the User needs to perform, but for the time being, just demo first)

  • DEAD DELETE REQUEST
    • The data returned by user 2 has been deleted from the list

3. Call the API on the Controller

  • After creating a simple API, we will call the API on the controller to display the View interface
  • Here, we must use a class call Request to be able to call the API, I will demo with HttpClient
    • Temporarily borrow the HomeController to display it on the Home page
    • We’ll use the async / await method so that we can handle the HttpClient asynchronously

  • In the View of HomeController I will edit as follows:

  • Run the Code and check the results

So Json raw data has been converted to List User and displayed as we want.

Probably reading this far, everyone thinks it’s done, right

  • BUT there is one problem that arises that anyone can call the API that triggers like that
  • NO, HERE WE DO NOT DO THIS: v
  • The fix here is to use Authorize to decentralize the use of the API (you can use JWT) => Maybe in the next post I will continue to this series but about the security and data binding, all

III/ End

  • Through this article, I have demonstrated how to create and call Restful API to process + put up the interface.
  • The article will have many errors, you can contribute more comments to make the following articles better.
  • Good luck!!!

References

Share the news now