ITZone

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

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.

I/ Restful API overview

1. Restful API Request Forms

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

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.

3. Advantages

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 !!

Create Controller part of WebAPI

 

2. API creation section

2.1. GET METHOD

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.

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

2.3. DELETE METHOD

3. Call the API on the Controller

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

References

Share the news now