HTTP

 HTTP

So far, most of our programs have worked with information that comes from sensors, buttons, displays, and devices connected directly to our microcontroller.

But what if we want information from the internet?

What if we want to:

  • Read weather data from a website
  • Send sensor readings to a cloud service
  • Retrieve information from an online database
  • Communicate with web applications
  • Connect an IoT project to the internet

To do these things, we use HTTP.


Introduction

When you open a website in a web browser, something ha

ppens behind the scenes.

Your device sends a request asking for information.

The website receives that request and sends information back.

The same thing can happen between a D1 R32 and a web server.

D1 R32

↓

HTTP Request

↓

Web Resource

↓

HTTP Response

↓

Program

This type of communication is the foundation of the modern internet.

Every day, websites, mobile apps, games, smart devices, and cloud services use HTTP to exchange information.


What Is HTTP?

HTTP stands for:

HyperText Transfer Protocol

While the full name sounds complicated, the basic idea is simple:

HTTP is a way for computers and devices to communicate across a network.

Think of HTTP as a conversation.

Your program asks a question.

The web server sends back an answer.


Everyday Example

Imagine asking a librarian for a book.

Request

You ask:

"Can I have the science encyclopedia?"

Response

The librarian replies:

"Here it is."

HTTP works in a very similar way.

Program

↓

Request

↓

Server

↓

Response

↓

Program


Why Use HTTP?

HTTP allows your projects to communicate with systems beyond the microcontroller itself.

Examples include:

  •  Weather services
  • Online databases
  • Cloud dashboards
  • Smart home systems
  • IoT platforms
  • Web applications
  • Educational APIs
  • Mobile apps

Without HTTP, your project can only work with nearby hardware.

With HTTP, your project can interact with the entire internet.


HTTP Category

[INSERT CATEGORY IMAGE HERE: HTTP Category]

Figure 12.1 - Blocks available in the HTTP category.

The HTTP category includes blocks for:

  • Sending requests
  • Storing responses
  • Reading response information
  • Retrieving text and JSON data
  • Checking status codes
  • Accessing headers and cookies

Understanding Requests and Responses

Before learning the blocks, it is important to understand the two most important HTTP concepts.


Request

A request is information sent from your program to a web resource.

The request may ask for information or send information.

Program

↓

Request

↓

Web Resource


Response

A response is the information returned by the web resource.

Web Resource

↓

Response

↓

Program


Complete Communication Cycle

Request

↓

Web Resource

↓

Response

Every HTTP interaction follows this pattern.


HTTP Request and Response Block

[INSERT BLOCK IMAGE HERE: HTTP Request + Response]

This block sends an HTTP request and stores the returned result inside a variable called:

response

Conceptually:

Send Request

↓

Receive Response

↓

Store Response

The stored response can then be examined using other HTTP blocks.


Why Store the Response?

The response contains important information.

For example:

  • Returned text
  • Response status
  • JSON data
  • Header information
  • Cookies

By saving the response, the program can use this information later.


HTTP Methods

When sending an HTTP request, one of several request methods can be selected.

Each method has a different purpose.


GET Request

urequests.get(...)

GET is the most common HTTP request.

Its purpose is simple:

Request information.


Example

D1 R32

↓

GET Request

↓

Weather Website

↓

Current Temperature

Most beginner projects start with GET requests.


Beginner Rule

 If you want information from the internet, start with GET.


POST Request

urequests.post(...)

POST sends information to a server.

Instead of asking for information, the device provides information.


Example

Temperature Sensor

↓

POST Request

↓

Cloud Database

A sensor reading could be sent to an online dashboard for storage.


PUT Request

urequests.put(...)

PUT is commonly used when updating existing information.


Example

Current Value

↓

Update Server Data

↓

Store New Value


DELETE Request

urequests.delete(...)

DELETE requests are used to remove information from a server.


Example

Request

↓

Delete Data

↓

Server Removes Data


HEAD Request

urequests.head(...)

A HEAD request asks for information about a resource without requesting the complete content.

This is a more advanced HTTP operation.

Students typically do not need it when first learning HTTP.


OPTION Request

urequests.option(...)

The OPTION request operation is provided by Clu-Blocks Pro.

Like HEAD, it is considered more advanced and is rarely needed in beginner projects.


Recommended Learning Progression

When learning HTTP:

  1. Learn GET
  2. Read the response text
  3. Check the status code
  4. Work with JSON
  5. Explore POST requests
  6. Learn PUT and DELETE
  7. Explore advanced methods

Reading a Response

[INSERT BLOCK IMAGE HERE: Response Content]

Once a request has been completed, the response can be examined.

The Response Content block allows you to choose which part of the response you want.

HTTP Request

↓

Response

↓

Choose Information

Available options include:

  • text
  • status_code
  • headers
  • cookies
  • content
  • json

Response Text

response.text

Text is often the easiest type of response for beginners to understand.

Conceptually:

Website

↓

Response

↓

Text

↓

Program


Example

HTTP Request

↓

Response Text

↓

Serial Port

↓

Computer Screen

Students can immediately see the information being returned.


Response Status Code

response.status_code

The status code tells the program whether the request was successful.

Conceptually:

Send Request

↓

Receive Response

↓

Check Status


Why Is This Important?

Imagine sending a request to a weather service.

If something goes wrong, the response may not contain the information you expected.

Checking the status code helps confirm whether the communication worked correctly.


Beginner Rule

 Always check the result before assuming a request succeeded.


Response Headers

response.headers

Headers provide extra information about the response.

Examples of information commonly found in headers include:

  • Content type
  • Server information
  • Data format
  • Communication details

Headers are useful for more advanced projects.


Response Cookies

response.cookies

Cookies are pieces of information associated with communication between systems.

Many websites use cookies to remember settings, preferences, or user-related information.

For beginner projects, cookies are considered an advanced topic.


Response Content

response.content

The content option provides access to the response's raw content.

This option becomes useful when working with files, images, binary data, or more advanced communication systems.


JSON Responses

response.json

JSON is one of the most common formats used to exchange information on the internet.

Many web APIs return information as JSON.


Example

A weather service might send:

Temperature: 22

Humidity: 65

Condition: Sunny

JSON helps organize information into a format that programs can easily understand.


Conceptual Flow

HTTP Request

↓

JSON Response

↓

Program Reads Data

↓

Display or Use Information


HTTP Requests Without Storing a Response

[INSERT BLOCK IMAGE HERE: HTTP Request]

The HTTP category also includes a request block that does not visibly save the response.

Instead of:

response = GET(...)

the request is simply performed:

GET(...)

When Should You Store the Response?

Store the response when you want to:

  • Read returned text
  • Check the status code
  • Process JSON data
  • Examine headers
  • Access cookies

When Might You Not Store It?

In simple situations where the program only needs to trigger an action and does not need information returned.

However, most beginner HTTP projects will benefit from storing the response.


HTTP Requires a Network Connection

HTTP communication only works when a network connection is available.

The overall process is:

D1 R32

↓

Wi-Fi

↓

HTTP Request

↓

Web Resource

↓

HTTP Response

↓

Program

Without a network connection, HTTP requests cannot reach their destination.


HTTP and the Serial Port

The Serial Monitor is an excellent learning tool when working with HTTP.

A recommended beginner workflow is:

Connect to Wi-Fi

↓

Send GET Request

↓

Receive Response

↓

Display Response Text

↓

Observe Results

Using the Serial Monitor allows students to see exactly what information is being returned.


HTTP in Internet of Things (IoT) Projects

HTTP is one of the technologies that makes IoT possible.

Example:

Temperature Sensor

↓

HTTP POST

↓

Cloud Service

↓

Store Reading

Or:

Weather API

↓

HTTP GET

↓

Receive Forecast

↓

Display on OLED Screen

This allows microcontrollers to interact with online services and real-world data.


Key Distinction: Request vs Response

Students often confuse these terms.


Request

Program → Server

The program asks for something.


Response

Server → Program

The server replies.


Memory Tip

 Request = Ask

 Response = Answer


Key Distinction: GET vs POST

Another common point of confusion is the difference between GET and POST.


GET

I want information.

Examples:

  • Weather data
  • Current time
  • Sensor history
  • Data from a website

POST

I want to send information.

Examples:

  • Sensor readings
  • Form submissions
  • Device updates
  • IoT measurements

Key Vocabulary

TermDefinition
HTTPA protocol used for communication over the web
RequestInformation sent by a program
ResponseInformation returned by a web resource
GETRequest information
POSTSend information
PUTUpdate information
DELETERemove information
Response TextText returned by a response
Status CodeIndicates the result of a request
HeaderAdditional response information
CookieInformation associated with communication
JSONA structured format for exchanging data

Summary

In this lesson, you learned how HTTP allows programs to communicate with web resources across a network.

You explored how to:

 Send HTTP requests

 Receive responses

 Use GET requests

 Store responses

 Read text data

 Check status codes

 Access JSON information

 Connect IoT projects to online services

HTTP is one of the most important technologies behind the internet. It enables microcontrollers, websites, mobile apps, cloud platforms, and smart devices to exchange information and work together.


Key Takeaway

HTTP allows a program to communicate with the internet by sending requests and receiving responses.

Once connected to a network, your Clu-Blocks projects can go beyond sensors and hardware, allowing them to retrieve information, share data, interact with cloud services, and become part of a larger connected world.