Learn / IoT and AI at the edge / Connected devices

Devices and Web APIs: HTTP, REST and JSON

ReviewedIntermediate MakerAbout 6 min
23.4°CSSD1306
In short

Devices and Web APIs: HTTP, REST and JSON: the short answer

Most web services talk HTTP. A REST API uses ordinary web addresses for resources and the HTTP method for the action: GET reads data, POST creates or sends it, PUT or PATCH updates it, DELETE removes it.

Level
Intermediate
Time
About 6 min
Topic
IoT and AI at the edge
You will learn to

Send and fetch data from web APIs with an ESP32 using HTTP requests, JSON and HTTPS, and handle errors properly.

Before this: Connecting an ESP32 to Wi-Fi, Connected Embedded Devices

Most web services talk HTTP. A REST API uses ordinary web addresses for resources and the HTTP method for the action: GET reads data, POST creates or sends it, PUT or PATCH updates it, DELETE removes it. The server replies with a status code: 200 means OK, 201 created, 400 a bad request, 401 not signed in, 404 not found, 429 too many requests and 500 a server error. Your device should check the code, not just assume the request worked.

Data usually travels as JSON, such as {"device":"tank-1","level":63,"battery":3.91}. On the ESP32, the HTTPClient library makes the request and ArduinoJson builds and parses JSON safely without fragile string handling. Send a POST with the header Content-Type: application/json and an API key in a header such as Authorization, never in the URL, where it can end up in logs.

Use HTTPS for anything that leaves your home network. WiFiClientSecure can check the server's certificate against a root CA certificate stored in your code, which prevents someone impersonating the server; setInsecure() skips the check and should only be used for quick tests. Design for failure: set a timeout (for example 5 s), retry with growing delays (2, 4, 8 s) instead of hammering the server, queue readings while offline, and respect 429 responses. Polling an API every second wastes battery and data; poll only as often as the data changes, or use MQTT when you need push updates.

Methods mean something

GET asks for data and should not change anything. POST sends data to create or trigger something. PUT and PATCH update, and DELETE removes. A device that turns on a relay from a GET link can be triggered by a browser pre-loading the page or by a web crawler, so use POST for actions.

The reply carries a status code: 200 means success, 400 means the request was wrong, 401 or 403 means not allowed, 404 means not found, and 500 means the server failed. A device should check the code before it trusts the body.

JSON on a small device

JSON text carries names and values, for example a temperature and a time. On a microcontroller, a JSON library needs memory, so size its buffer for the largest message you expect and check for a parse error. Keep messages small: a device that posts every second with long text wastes power and bandwidth.

Add a timeout to every request. A call that waits forever on a bad network freezes the whole loop.

HTTPS and keys

Send secrets such as an API key only over HTTPS, never in a plain HTTP link that could be seen on the way. A key in a sketch you share is a key given away, so keep it in a separate file that is not uploaded.

Common mistakes
  • Using GET for an action such as switching a relay.
  • Making a request without a timeout and freezing the device.
  • Publishing an API key inside shared code.
Intermediate lesson · Maker plan

Practise this lesson with Maker

The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.

With Maker you also get: a worked example, a hands-on task, a 3-question check.

Common questions

What will I learn in "Devices and Web APIs: HTTP, REST and JSON"?

Send and fetch data from web APIs with an ESP32 using HTTP requests, JSON and HTTPS, and handle errors properly.