# HTTP Responses & Status Codes # HTTP Responses * HTTP responses have the following format: * Status line: HTTP version, status code, reason phrase * HTTP/1.1 200 OK * Headers (optional) * Date: Mon, 07 Sep 2020 08:39:33 GMT * Content-Type: application/json * Body (optional) * { * "date": "2020-09-08T11:39:34.2837813+03:00", * "temperatureC": 11, * "temperatureF": 51, * "summary": "Cool" * } # Status Codes * The status line of HTTP a response sent by an API should accurately describe the status of what has happened on the server after each request: * Did the operation succeed? (2xx status codes) * Was there an error with the request, i.e. the request line was malformed, or the server doesn't support it (4xx status codes) * Did a server side exception occur? (5xx status codes) * This is never the client's fault # Status Codes (continued) Your APIs should use the following status codes for responses to CRUD operations: | __2xx SUCCESS__ | __4xx CLIENT ERROR__ | __5xx SERVER ERROR__ | | :-: | :-: | :-: | | 200 - OK | 400 - Bad Request | 500 - Internal Server Error | | 201 - Created | 401 - Unauthorized | | | 204 - No Content | 403 - Forbidden | | | | 404 - Not Found | | | | 409 - Conflict | | [https://restfulapi.net/http-status-codes/](https://restfulapi.net/http-status-codes/) # ASP.NET Status Codes * There are multiple possible status codes for each action * The ControllerBase which the controllers should inherit from includes __result methods__ for creating ActionResult objects * All ActionResult objects implement IActionResult * Includes at least a status code, and can contain data such as view items or an error message * This result is processed into a response to then send to client # ASP.NET Result Methods | __Status Code__ | __Result Method__ | __Use__ | | :-: | :-: | :-: | | __200 - OK__ | __Ok()_ | __GET, DELETE__ | | __201 - Created__ | __Created()_ | __POST__ | | __204 - No content__ | __NoContent()_ | __PUT, PATCH__ | | __400 - Bad request__ | __BadRequest()_ | __POST, PUT, PATCH__ | | __404 - Not found__ | __NotFound()_ | __All actions__ | | __Custom__ | __StatusCode()_ | __All actions__ | # ASP.NET Status Codes (continued) [HttpGet("{id}")] public IActionResult GetContactById(int id) { // Contacts = list of contact objects, fetched from some repository var contact = Contacts.FirstOrDefault(c => c.Id == id); if (contact == null) { return NotFound(); } return Ok(contact); } The previous code first attempts to find a Contacts object using the ID provided in the URI parameter If an item is not found, a 404 response is returned using NotFound() Otherwise, the object is sent as a payload with a 200 OK code response using Ok(contact) ![](imgs/3-http-responses-and-status-codes_0.png) ![](imgs/3-http-responses-and-status-codes_1.png) ![](imgs/3-http-responses-and-status-codes_2.png) ![](imgs/3-http-responses-and-status-codes_3.png) # Exercise 1: Returning Status Codes Without using parameter constraints, modify your number list method from Lecture 2, Exercise 2 to return a status code 400 (Bad Request) if _k_ is smaller than one or larger than 100. Add a helpful error message to the result for both cases. The message should be seen as a message body in the response. Test with Swagger/Postman.