# HTTP Requests

### Connecting to the Internet

So far, the data that we have used have been hard-coded on the frontend. For example, in our A3: ChatDev assignment, the `Post` objects that are displayed in the table view are hard-coded inside of our code. There is an array of `Post` objects written in Swift code known as **dummy data**. In other words, these posts are expected to be fetched from the internet and displayed onto our app. If a different user creates a post, then we want it to be updated on our side.

### Clients and Servers

A **client** is a device or software that **requests** a service. In this course, the client is our iOS app because it **requests** information from some service. Who provides this service? That’s the job of a server. A **server** is a device or software that **responds** to clients with these services. Remember, clients send a ***request*** and a server **responds**.

### What is an HTTP request?

An HTTP request is a method used to communicate between a **client** and a **server**. There are many different types of HTTP requests; however, the most common types are `GET`, `POST`, `PUT`, and `DELETE`. **For the scope of this course, we will only discuss a `GET` and a `POST` request** (and maybe `DELETE`).

These requests are commonly paired with a **CRUD** operation. CRUD stands for **CREATE, RETRIEVE, UPDATE,** and **DELETE**. This idea is often used when dealing with models. For example, we can **retrieve** posts on Instagram, **create** a post on Instagram, etc.

### HTTP Response Codes

So, the client sends an HTTP request to a server asking for some service. What can the server respond with? Well, the server can respond with the service the client is requesting, but we don’t live in a perfect world and things can go wrong! For example, when we click on a website, as clients, we are sending an HTTP request to the web server to provide us with the website information. But what if our WiFi connection gets cut as we are sending the request? How will the server communicate with us about what went wrong?

An **HTTP response code** (or status code) indicates whether an HTTP request has been successfully completed and provides information about the request. Have you ever went to a website and received a “404 Not Found” error? Well, this “404” is an HTTP response code indicating that the server could not find what the client was looking for.

HTTP response codes range from 1XX to 5XX, with the first digit indicating the category:

<figure><img src="/files/vzgecn3PF4UQxYBHtME8" alt=""><figcaption><p><em>Source:</em> <a href="http://Semrush.com"><em>Semrush.com</em></a></p></figcaption></figure>

{% hint style="info" %}
I**n this course, we will focus primarily on 2XX and 4XX response codes and will not pay too much attention to the specific XX part of the code.** For a full list, check out this [website](https://www.semrush.com/blog/http-status-codes/).
{% endhint %}

### Storing and Transporting Data with JSON

So far, we’ve learned that clients send an HTTP request and a server responds back with a status code. However, clients and servers need to understand the data that is being transmitted. The issue is that most of the time, client-side and server-side code are written in different languages. For example, our code may be written in Swift, but the backend server may not be able to recognize it. Then, how are we able to send this data? We use JSON.

**JSON (JavaScript Object Notation)** is a text-based data format that is language-independent. The powerful thing about JSON is that it is widely used and many modern programming languages and frameworks have built-in functionality to parse data in JSON format. JSONs have **key-value** pairs, just like dictionaries in Swift. **The keys are always strings, but a value can be a string, number, object, array, boolean, or null.**

Consider the following `Student` model:

```swift
struct Student {
    let age: Int
    let classes: [String]
    let major: String
    let name: String
}
```

In Swift, if we wanted to create this object, we could write the following code:

```swift
let vin = Student(age: 19, classes: ["CS 1998", "PHYS 2213"], major: "Info Sci", name: "Vin Bui")
```

Now, this object is currently stored locally on the frontend. If we wanted to fetch this from the backend, then the server response can represent the data as a JSON (notice that this is very similar to a dictionary in Swift):

```json
{
    "age": 19,
    "classes": [
	"CS 1998",
	"PHYS 2213"
    ],
    "major": "Info Sci",
    "name": "Vin Bui"
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/textbook/networking-i/http-requests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
