{"id":151,"date":"2023-09-23T16:46:47","date_gmt":"2023-09-23T13:46:47","guid":{"rendered":"https:\/\/codeblog.xyz\/?p=151"},"modified":"2023-10-23T00:46:34","modified_gmt":"2023-10-22T21:46:34","slug":"rest-api-20-basic-consepts","status":"publish","type":"post","link":"https:\/\/codeblog.xyz\/?p=151","title":{"rendered":"REST API \u2013 20 Basic Consepts"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Stateless<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: In REST, each API call is independent and contains all the information needed for its execution.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">HttpClient client = new HttpClient();\nvar response = await client.GetAsync(&quot;https:\/\/api.example.com\/users\/1&quot;);<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Client-Server Architecture<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: REST APIs operate on a client-server model, separating the client logic from the server logic.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Creating a Web API server and consuming it using <code>HttpClient<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Uniform Interface<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: RESTful APIs follow a uniform interface, making it easier to understand and interact with them.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>GET, POST, PUT, DELETE HTTP methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Resource-Based<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: In REST, everything is considered a resource and is accessed via its resource URL.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">[HttpGet(&quot;api\/users\/{id}&quot;)]\npublic ActionResult GetUser(int id) { \/* ... *\/ }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. Layered System<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: REST allows you to have multiple layers (e.g., load balancing, security) between client and server.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using middleware in ASP.NET Core for caching, authentication, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Cacheability<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: RESTful APIs explicitly indicate whether the client can cache the resource.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using HTTP headers like <code>Cache-Control<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Media Types<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: RESTful APIs can return different media types like JSON, XML, etc.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">[Produces(&quot;application\/json&quot;)]\npublic class UsersController : Controller { \/* ... *\/ }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">8. Versioning<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: APIs often have multiple versions to support backward compatibility.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using URL paths like <code>\/v1\/users<\/code> and <code>\/v2\/users<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. Error Handling<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: REST APIs return standardized HTTP status codes to indicate errors.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">return NotFound(&quot;User not found&quot;);<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">10. Pagination<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Paginating large sets of resources is a common practice in RESTful APIs.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Adding query parameters like <code>?page=1&amp;limit=10<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. Filtering and Sorting<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: REST APIs often allow clients to filter and sort data using query parameters.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public ActionResult GetUsers([FromQuery] string sort) { \/* ... *\/ }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">12. HATEOAS<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Hypermedia as the engine of application state; navigating APIs through hypermedia links.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Returning URLs for related resources in API responses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. Idempotence<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Certain operations (GET, PUT, DELETE) produce the same result, even if repeated multiple times.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>A DELETE request that deletes a resource once and then has no effect.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14. Rate Limiting<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Limiting the number of requests a client can make in a given time period.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using middleware to enforce rate limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. Authentication &amp; Authorization<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Ensuring that only authorized clients can access resources.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>JWT, OAuth2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">16. Logging and Monitoring<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Tracking API usage and errors for debugging and analytics.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using logging frameworks like Serilog.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">17. ETags<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: ETags are used for conditional requests, reducing unnecessary data transfer.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Using <code>If-None-Match<\/code> header.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">18. Asynchronous Processing<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Some tasks can be processed asynchronously and their state can be polled later.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Returning a <code>202 Accepted<\/code> status and a URL to check the processing status.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">19. Request and Response Headers<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: Headers are used to convey metadata for both request and response.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br>Custom headers for tracking or authentication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">20. URI Design<\/h3>\n\n\n\n<p><strong>Explanation<\/strong>: The design of URIs should be intuitive and convey the resource hierarchy.<\/p>\n\n\n\n<p><strong>C# Example<\/strong>:<br><code>\/users\/1\/orders\/2<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Pros and Cons of REST Architecture<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Pros<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simplicity<\/strong>: REST is straightforward to understand and implement.<\/li>\n\n\n\n<li><strong>Scalability<\/strong>: Stateless servers enable excellent scalability.<\/li>\n\n\n\n<li><strong>Flexibility<\/strong>: It supports multiple types of data formats.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Cons<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overhead<\/strong>: Stateless interactions can involve more data transfer.<\/li>\n\n\n\n<li><strong>Limited Functionality<\/strong>: REST is less suitable for operations that can&#8217;t be easily mapped to HTTP verbs.<\/li>\n\n\n\n<li><strong>Learning Curve<\/strong>: While REST is simple, it requires a deep understanding to use effectively.<\/li>\n<\/ul>\n\n\n\n<p>I hope you find this deep dive into RESTful API architecture useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Stateless Explanation: In REST, each API call is independent and contains all the information needed for its execution. C# Example: 2. Client-Server Architecture Explanation: REST APIs operate on a client-server model, separating the client logic from the server logic. C# Example:Creating a Web API server and consuming it using HttpClient. 3. Uniform Interface Explanation: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":2,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}