---
marp: true
paginate: true
math: mathjax
theme: buutti
title: 2. HTTP Responses
---

# HTTP Responses

<!-- headingDivider: 5 -->
<!-- class: invert -->

## ASP.NET Status Codes

* For a primer on HTTP status codes, check [Frontend basics: Lecture 2](https://gitea.buutti.com/education/frontend-basics/src/branch/main/2-http.md#http-status-codes)

* There are multiple possible status codes for each action
* The `ControllerBase` the controllers should inherit from includes  *__result methods__* for creating `ActionResult` objects
  * All `ActionResult` objects implement the `IActionResult` interface
  * 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 sent to client

### Result methods

| Status Code         | Result Method     | Usage                          |
|:-------------------|:-----------------|:----------------------------|
| `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`                |


### Status code example

```csharp
[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()`
![w:700](imgs/3-http-responses-and-status-codes_0.png) ➡ ![](imgs/3-http-responses-and-status-codes_1.png)
  * Otherwise, the object is sent as a payload with a 200 OK code response using `Ok(contact)`
![w:700](imgs/3-http-responses-and-status-codes_2.png) ➡ ![](imgs/3-http-responses-and-status-codes_3.png)

## Exercise 1: Returning status codes
<!--_class: "exercise invert" -->

1) Without using parameter constraints, modify your number list method from [Lecture 1, Exercise 3](1-aspnet-introduction#exercise-3-returning-your-own-list) to return a status code `400 (Bad Request)` if $k$ is smaller than $1$ or larger than $100$.
2) Add a helpful error message to the result for both cases. The message should be seen as a message body in the response.
3) Test with Swagger/Postman.

