Sub topic 6
Making an HTTP Request
This chapter assumes you can upload an Arduino sketch, use setup(), loop(), and the Serial Monitor, and connect a Wi-Fi board to a network. It also assumes that your project already has an arduino_secrets.h file containing SECRET_SSID and SECRET_PASS.
An HTTP request lets your Arduino ask a web server for a resource. HTTP is an application protocol that uses a TCP connection. The board resolves the server name with DNS, opens a TCP connection to port 80, sends request text, and reads the server's response.
The request sequence
A WiFiClient object manages the TCP connection. The connect() call accepts a host name and port. The Wi-Fi module uses DNS to turn the host name into an IP address.
An HTTP/1.1 request needs a request line, a Host header, and a blank line that marks the end of the headers:
GET / HTTP/1.1
Host: example.com
Connection: close
The Connection: close header asks the server to close the connection after sending the response. That gives a small embedded sketch a clear stopping point.
Complete HTTP client sketch
This example connects to Wi-Fi, requests the root resource from example.com, and prints the raw response. The response normally contains a status line, headers, a blank line, and the response body.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiClient client;
const char host[] = "example.com";
const unsigned long RESPONSE_TIMEOUT_MS = 5000;
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi...");
delay(5000);
}
Serial.println("Wi-Fi connected");
if (!client.connect(host, 80)) {
Serial.println("Connection to server failed");
return;
}
client.println("GET / HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
unsigned long lastData = millis();
while (client.connected() &&
millis() - lastData < RESPONSE_TIMEOUT_MS) {
while (client.available()) {
Serial.write(client.read());
lastData = millis();
}
}
if (millis() - lastData >= RESPONSE_TIMEOUT_MS) {
Serial.println("\nResponse timeout");
}
client.stop();
}
void loop() {}client.println() sends a line ending suitable for HTTP. The final client.println() sends the empty line required after the headers. client.available() reports how many bytes can be read without waiting. Serial.write() prints each byte exactly as received, including HTML and line endings.
>callout note > HTTP on port 80 is unencrypted. Anyone who can observe the network may read the request and response. Use WiFiSSLClient and port 443 for HTTPS when the data needs confidentiality or server authentication.
Common errors
- Missing
Hostheader: HTTP/1.1 servers commonly reject the request or return an error. - Missing blank line: The server keeps waiting for more headers.
- Wrong port: HTTP normally uses port 80. HTTPS normally uses port 443.
- Using an IP without checking the server: Some services use the host name to select the correct site.
- Reading only once: Network data can arrive in several pieces. Keep reading while bytes are available.
- Waiting forever: A server or network can fail without closing the connection. Use a timeout.
- Expecting JSON automatically:
WiFiClientreturns bytes. Your sketch must locate the header/body boundary and parse JSON separately.
A successful response begins with a status line such as HTTP/1.1 200 OK. A 200 status means the server handled the request successfully. A 404 means the requested path was not found, and a 500 indicates a server-side failure.
Exercise
Why does the sketch send both a Host header and a blank line after the request headers?
Show answer
The Host header identifies the web site requested by the client. The blank line tells the server that the request headers are complete.