STM32 HTTP Client Over WiFi: GET, POST and JSON (Examples)

In this tutorial, we’ll discuss how to build an STM32 HTTP client over WiFi to send HTTP GET and POST requests and parse JSON replies, using an ESP32-C3 as a WiFi co-processor. You’ll learn how the ESP32-C3’s built-in HTTP client (ESP-AT) works, how to send GET and POST requests from an STM32 Blue Pill over plain HTTP and HTTPS, and how to parse a JSON reply on the STM32 with cJSON, jsmn, or a few lines of your own code.

Then, we’ll build three STM32 HTTP client example projects with our ESP_AT driver: HTTP and HTTPS GET requests, a JSON POST of a sensor reading, and a weather client that parses a real JSON API reply. We’ll also measure how long each request takes and compare the three JSON parsers on the STM32. This is the fourth part of our STM32 WiFi tutorial series. Without further ado, let’s get right into it!

STM32 WiFi Tutorial Series: Part 4 of 8
1 STM32 WiFi2 STM32 ESP82663 STM32 ESP32-C34 HTTP Client5 Web Server6 MQTT7 OTA Update8 WebSocket

Note: The STM32 code examples in this tutorial are tested with: STM32 Blue Pill (STM32F103C8T6), ESP32-C3 Core Board with ESP-AT firmware, cJSON, and jsmn.


STM32 HTTP Client: How It Works

An HTTP request is a few lines of text sent over a TCP connection, and the server answers with a status line, headers, and a body (often JSON). On a microcontroller with its own TCP/IP stack, you’d build the request, open the TCP (or TLS) connection, and parse the reply headers yourself. With an ESP32-C3 running ESP-AT, all of that runs on the ESP32-C3: the STM32 sends one AT command with the URL, and it receives only the body.

StepWho Does ItAT Command
DNS lookup, TCP connection, TLS handshake (HTTPS)ESP32-C3–
GET requestSTM32 sends one commandAT+HTTPCGET="<url>"
POST request with a short bodySTM32 sends one commandAT+HTTPCLIENT=3,<type>,"<url>",,,<1/2>,"<data>"
POST request with a long or binary bodySTM32 sends the command, then the body after the > promptAT+HTTPCPOST="<url>",<length>
Reply bodyESP32-C3 sends it to the STM32 in frames+HTTPCGET:<n>,<data>, +HTTPCLIENT:<n>,<data>
JSON parsingSTM32–

The STM32 side is our ESP_AT driver from the ESP32 AT commands with STM32 tutorial (wiring, CubeMX configuration, and the driver basics are all there). For this tutorial, the driver gained two new jobs: ESP_AT_HttpGet() and ESP_AT_HttpPost(). They pick the right AT command by themselves, read the body frames by their length, and hand the body to your code in chunks.

STM32 HTTP client architecture: the STM32 sends AT commands to the ESP32-C3, which runs the HTTP client, TLS, and the TCP/IP stack


STM32 HTTP Client Wiring and CubeMX Configuration

Required Parts

QTY.Component NameAmazon.comAliExpress
1STM32 Blue Pill (STM32F103C8T6)AmazonAliExpress
1ST-Link V2 DebuggerAmazonAliExpress
1ESP32-C3 board (ESP32-C3 SuperMini; we use the WeAct ESP32-C3 Core Board)AmazonAliExpress
1USB-TTL (UART) Converter, 3.3VAmazonAliExpress
1BreadBoardAmazonAliExpress
1Jumper Wires PackAmazonAliExpress

STM32 ESP32-C3 Wiring

The wiring is the same as in the previous tutorial: the STM32’s USART1 goes to the ESP32-C3’s AT port, and USART2’s TX pin goes to a USB-TTL converter for the log. The ESP32-C3 needs the ESP-AT firmware (the previous tutorial shows how to flash it).

PA9 USART1 TX, goes to the ESP32-C3 GPIO6 (AT RX).

PA10 USART1 RX, goes to the ESP32-C3 GPIO7 (AT TX).

PA2 USART2 TX, goes to the USB-TTL converter’s RX: the log to your PC at 115200 baud.

GND Goes to the ESP32-C3 GND and the USB-TTL GND: common ground.

STM32 HTTP client wiring: Blue Pill PA9 to ESP32-C3 GPIO6, PA10 to GPIO7, and PA2 to a USB-TTL converter

STM32 Blue Pill ESP32-C3 / USB-TTL PA9 (USART1 TX) GPIO6 (AT RX) PA10 (USART1 RX) GPIO7 (AT TX) GND GND PA2 (USART2 TX) USB-TTL RX GND USB-TTL GND

STM32 CubeMX Configuration

The CubeMX configuration is also the same: USART1 at 115200 baud with RX DMA in Circular mode and the USART1 global interrupt for the ESP32-C3, USART2 for the log, PC13 as the LED output, and the 72MHz clock. The ESP32 AT commands tutorial shows every step with screenshots. This is the DMA setting that matters most:

STM32CubeMX USART1 DMA Settings: USART1_RX in Circular mode and USART1_TX in Normal mode for the ESP_AT driver

Note: For the cJSON example, increase the heap in CubeMX (Project Manager > Linker Settings > Minimum Heap Size) from the default 0x200 to 0x1000. cJSON allocates its tree with malloc, and 0x200 (512 bytes) is too small for a typical API reply.


STM32 HTTP Client Functions (ESP_AT Driver)

These are the two new driver functions. Both are non-blocking jobs that end with a ESP_AT_EVT_JOB_DONE event, and both have a _Blocking version for simple programs:

FunctionWhat It Does
ESP_AT_HttpGet(&esp, url)GET request over HTTP or HTTPS (from the URL). Long URLs (over 160 characters) go through AT+HTTPURLCFG by themselves
ESP_AT_HttpPost(&esp, url, contentType, body, len)POST request. Short one-line bodies use AT+HTTPCLIENT, longer or binary bodies use AT+HTTPCPOST
ESP_AT_EVT_HTTP_DATA eventThe reply body, in chunks of up to 256 bytes: d->data, d->len, and d->remaining
esp.httpStatus200 on success, the HTTP error code (404, 500…) on failure, 0 if there was no connection
esp.httpBodyLenThe number of body bytes received

ESP-AT doesn’t report the HTTP status code on success, only on failure (as an ERR CODE line with AT+SYSLOG=1, which the driver turns on). So esp.httpStatus is 200 whenever the request worked. The table below lists the other limits we found in ESP-AT v4.1.1.0, measured on our ESP32-C3 and checked against Espressif’s HTTP AT command documentation:

TopicWhat ESP-AT v4.1.1.0 Does
Redirects (3xx)Not followed. The request returns OK with the redirect page as the body, so request the final URL
Custom headersAT+HTTPCHEAD sets headers for all later requests (not wrapped by the driver yet)
HTTPS certificatesNot checked by the default firmware: HTTPS here is encrypted, but the server is not authenticated
Connection reuseNone: every request opens a new connection, so every HTTPS request repeats the TLS handshake
Largest body tested60,000 bytes in 5.7 seconds (limited by the 115200 baud UART)
Longest URL tested220 characters (through AT+HTTPURLCFG)

STM32 HTTP Client Example 1: HTTP GET Request

In the first example project, the STM32 joins WiFi and sends two GET requests: one over plain HTTP to httpbin.org (a test service that echoes the request back), and one over HTTPS to the Open-Meteo weather API, which needs no API key. It prints each reply body, its size, and how long the request took.

Wiring: PA9 to ESP32-C3 GPIO6, PA10 to GPIO7, PA2 to the USB-TTL RX, and GND to both.

Write your WiFi name and password in Core/Inc/wifi_secrets.h:

Here is The Application Code For This Example Project (main.c). Only the USER CODE sections are shown; the rest of the file is generated by CubeMX:

STM32 HTTP GET Example Code Explanation

OnEvent() receives the reply body as ESP_AT_EVT_HTTP_DATA events and prints each chunk as it arrives, so the STM32 never needs a buffer as big as the whole reply. Get() calls ESP_AT_HttpGet_Blocking(), which sends AT+HTTPCGET and returns when the whole body has arrived. The URL decides HTTP or HTTPS, nothing else changes.

If the module runs the old ESP8266 NonOS AT firmware, which has no HTTP commands, ESP_AT_HttpGet() returns ESP_AT_ERROR_UNSUPPORTED, and the example falls back to RawTcpGet(): it opens a TCP connection to port 80 and writes the HTTP request text itself. That’s how an HTTP request looks on the wire, and it works on any AT firmware.

STM32 HTTP GET Example Testing

This is the real hardware testing result for this example project on my Blue Pill and ESP32-C3 (my public IP address in httpbin’s "origin" field is hidden):

STM32 HTTP client Example 1 serial log: HTTP and HTTPS GET requests with the body, status, size, and time
STM32 HTTP client Example 1 serial log: HTTP and HTTPS GET requests with the body, status, size, and time

The plain HTTP GET took about half a second. The HTTPS GET to Open-Meteo took about 2 seconds, because the ESP32-C3 sets up a new TLS session for every request.


STM32 HTTP Client Example 2: HTTP POST Request (JSON)

In this example project, the STM32 builds a small JSON document from a sensor reading and POSTs it over HTTPS to httpbin.org every 15 seconds. httpbin sends back what it received, so we can see that the server got valid JSON with the right Content-Type. In a real project, the same call sends your data to your own server, a REST API, or a cloud dashboard.

Here is The Application Code For This Example Project (main.c). Only the USER CODE sections are shown; the rest of the file is generated by CubeMX:

STM32 HTTP POST Example Code Explanation

The JSON body is built with snprintf(), which is all you need to send JSON: no library, no heap. ESP_AT_HttpPost_Blocking(&esp, url, "application/json", body, len) sends it. A short one-line body like this one goes in a single AT+HTTPCLIENT command (the whole command must stay under 256 bytes, with quotes and commas escaped, which the driver does). A longer body, or any body with line breaks, goes through AT+HTTPCPOST: the driver sends the length, waits for the > prompt, and then sends the raw body.

STM32 HTTP POST Example Testing

This is the real hardware testing result for this example project:

STM32 HTTP client Example 2 serial log: HTTPS POST of a JSON reading every 15 seconds
STM32 HTTP client Example 2 serial log: HTTPS POST of a JSON reading every 15 seconds

httpbin parsed our body as JSON (the "json" field) and saw the Content-Type: application/json header. The HTTPS POST took 2.2 seconds. Over plain HTTP, the same POST took about 0.7 seconds.


STM32 HTTP Client Example 3: Parse JSON on STM32 (cJSON)

In this example project, the STM32 asks the Open-Meteo API for the current temperature in New York once a minute, parses the JSON reply, and prints the temperature and the time. The reply looks like this (shortened):

The example has three JSON parsers, selected with JSON_PARSER at the top of the code, so you can compare them on your own STM32:

  • 1 = cJSON (MIT license): parses the whole reply into a tree on the heap, then you read any field by name. The easiest to use.
  • 2 = jsmn (MIT license): a tokenizer with a fixed token array and no malloc. You walk the tokens to find a key.
  • 3 = own extractor: a few lines of strstr() for one known reply format, no library at all.

To add cJSON to your project, copy cJSON.c and cJSON.h into the project (the example has them in a JSON folder with their license files), add the folder to the include paths, and increase the heap as described above.

Here is The Application Code For This Example Project (main.c). Only the USER CODE sections are shown; the rest of the file is generated by CubeMX:

STM32 JSON Parser Example Code Explanation

OnEvent() collects the body chunks into a 1KB buffer. With cJSON, Parse() calls cJSON_Parse(), reads "current", then "temperature_2m" and "time" inside it with cJSON_GetObjectItemCaseSensitive(), and frees the tree with cJSON_Delete(). The example plugs counting allocators into cJSON ( cJSON_InitHooks()), so it can print how much heap the parse really used.

STM32 JSON Parser Example Testing

This is the real hardware testing result for this example project with cJSON:

STM32 HTTP client Example 3 serial log: weather JSON parsed on the STM32 with cJSON
STM32 HTTP client Example 3 serial log: weather JSON parsed on the STM32 with cJSON

cJSON vs jsmn vs Own Parser on STM32

We built the same example with each parser (Release build) and parsed the same 325-byte Open-Meteo reply on the Blue Pill at 72MHz:

ParserExtra FlashExtra RAMParse TimeBest For
Own extractor (strstr)baseline (20.5KB app)none103µsOne known, fixed reply format
jsmn+0.9KB+768 bytes (48 tokens)675µsSmall MCUs, no heap, many fields
cJSON+14.9KB (about 6KB of it is strtod, double math, and malloc)heap peak 888 bytes1.56msEasy code, nested JSON, building JSON too

All three are fast enough: even cJSON parses the reply in 1.56ms, while the HTTPS request itself takes about 2 seconds. The real cost is flash: cJSON adds 14.9KB, which is a lot on a 64KB STM32F103C8. For a Blue Pill reading a few fields from one API, jsmn is the best balance. For a bigger STM32, or JSON you build and change, cJSON is the most comfortable.


STM32 HTTP Client Measured: Request Times

These are the times we measured on the STM32, from sending the AT command to the end of the reply body, with the ESP32-C3 at 115200 baud:

RequestHTTPHTTPS
GET (httpbin, Open-Meteo, about 200 to 300 bytes)0.37 to 0.55s1.6 to 2.3s
POST, short JSON body (AT+HTTPCLIENT)0.7s2.2 to 2.5s
POST, 1.2KB body (AT+HTTPCPOST)1.0s2.75s
GET, 60,000-byte body5.7s (the UART at 115200 is the limit)–

HTTPS adds about 1.5 seconds to every request, because the ESP32-C3 opens a new TLS session each time. If your device sends data often, send it in batches, or use MQTT (a later part of this series), which keeps one connection open.

STM32 HTTP vs HTTPS on The Oscilloscope

Here’s Example 1 on my oscilloscope captures. CH1 (yellow) is PA9, the STM32’s TX line to the ESP32-C3, and CH3 (blue) is PA10, the ESP32-C3’s TX line back to the STM32.

After the module check and the WiFi join, the STM32 sends the HTTP GET command, and the reply body comes back 0.37 seconds later. Then it sends the HTTPS GET command: the cursors show that its reply came 1.55 seconds later, and the UART lines stay idle for all that time while the ESP32-C3 does the TLS handshake:

Oscilloscope capture of STM32 HTTP and HTTPS GET requests through an ESP32-C3: the HTTP reply comes 0.37s after the command and the HTTPS reply 1.55s after
Oscilloscope capture of STM32 HTTP and HTTPS GET requests through an ESP32-C3: the HTTP reply comes 0.37s after the command and the HTTPS reply 1.55s after


STM32 HTTP Client Troubleshooting

The request returns ERROR with no status code

The ESP32-C3 couldn’t connect: check that WiFi is joined, that the URL starts with http:// or https://, and that the server is reachable. Some servers also refuse the ESP-AT TLS setup: in our tests, worldtimeapi.org failed every time while other HTTPS APIs worked.

I get a 3xx page instead of the data

ESP-AT doesn’t follow redirects. Request the final URL (for example, https:// instead of http://, or with the trailing slash the server expects).

cJSON_Parse() returns NULL

Usually the heap is too small (increase it to 0x800 to 0x1000 in CubeMX), or the body was cut because your receive buffer is smaller than the reply. Print the body first.

The HTTPS request takes 2 seconds

That’s the TLS handshake on the ESP32-C3, repeated for every request. Use plain HTTP on your local network, or send less often.

The POST body arrives broken or the command returns ERROR

An AT+HTTPCLIENT POST must fit in one 256-byte command, on one line. For longer bodies, use AT+HTTPCPOST (our driver switches automatically).

HTTP doesn’t work with my ESP8266

The ESP8266 NonOS AT firmware has no HTTP commands. Use a TCP connection and write the request text yourself, as the fallback in Example 1 does, or use an ESP32-C3.


STM32 HTTP Client FAQ

Can an STM32 send HTTP requests without an ESP32?

Yes, if the STM32 has a network interface and a TCP/IP stack, for example, an STM32F4/F7/H7 with Ethernet and LwIP, or an STM32 with a W5500 Ethernet chip. Over WiFi, the simplest way is an ESP32 co-processor like in this tutorial.

Is HTTPS secure with ESP-AT?

The data is encrypted, but the default ESP-AT firmware doesn’t verify the server’s certificate, so it can’t detect a fake server. For real security, build ESP-AT with certificate checking and load the server’s CA certificate.

Which JSON library is best for STM32?

For small MCUs like the Blue Pill, jsmn: under 1KB of flash and no heap. For larger STM32s, or when you also build JSON, cJSON is easier to use. Our measurements are in the comparison table above.

How do I send sensor data to a server from STM32?

Build a JSON string with snprintf() and POST it, as in Example 2. Point the URL at your server or a cloud service’s REST API, and send an API key in the URL or a header if the service needs one.


STM32 WiFi Tutorial Series

This tutorial is the fourth part of our STM32 WiFi tutorial series:

PartTutorialWhat You’ll Build
1STM32 WiFiChoosing a WiFi module, quick start, ESP32-C3 vs ESP8266 measured
2ESP8266 AT Commands With STM32ESP8266 AT firmware, commands, and our ESP_AT driver
3ESP32 AT Commands With STM32ESP-AT firmware, commands, SNTP time, and our ESP_AT driver
4STM32 HTTP Client Over WiFi (this tutorial)GET, POST, HTTPS, and JSON parsing
5STM32 Web ServerControl GPIOs and read sensors from a browser
6STM32 MQTTPublish and subscribe, Home Assistant
7STM32 OTA Firmware UpdateUpdate the STM32’s firmware over WiFi
8STM32 WebSocketReal-time data with an ESP32 smart bridge
Next: Part 5, Web Server (coming soon) →

Download STM32 HTTP Client Example Projects (GET, POST, JSON)

Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.

Wrap Up

To conclude this tutorial, we’ve built an STM32 HTTP client over WiFi with an ESP32-C3 and our ESP_AT driver. We sent HTTP and HTTPS GET requests, POSTed a JSON sensor reading, and parsed a real JSON API reply on the STM32 with cJSON, jsmn, and a few lines of our own code. The measurements showed that HTTPS adds about 1.5 seconds per request, and that jsmn is the best JSON parser for a small STM32 like the Blue Pill.

In the next part of this series, we’ll turn it around and build an STM32 web server that you can open from a browser to control GPIOs and read sensors.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment