Struggling with HTTPS request header on POST to REST API database

patricia
Posts: 2
Joined: Tue Mar 18, 2025 11:35 pm

Struggling with HTTPS request header on POST to REST API database

Postby patricia » Wed Mar 19, 2025 3:22 pm

Hello! I tried posting this question but it didn't seem to appear in the forum so posting again.

I am trying to use an ESP32-C6-MINI to give my STM32 internet connectivity so it can post engineering data (think motor speed, current, etc.) to an online database that uses REST API. I programmed the ESP with the released ESP-AT firmware.

For testing I have been using the ESP32 C6 devkit and the free restdb.io website. Below is working Python code to accomplish the posting of a speed value. This was run on my PC -

Code: Select all

import requests
import json

url = "https://test1-dec1.restdb.io/rest/pumpdata"

payload = json.dumps( {"speed": "23000"} )
headers = {
    'content-type': "application/json",
    'x-apikey': "xxxxxxxxx",
    'cache-control': "no-cache"
    }

response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
print(response.url)
I have not been able to replicate this using the ESP-AT HTTPS commands. I was able to do a simple GET from a cat fact API using -

Code: Select all

AT+HTTPCLIENT=2,1,"https://catfact.ninja/fact",,,2
But all the variations I have tried where I send HTTPS request headers has returned "ERROR". I guess it doesn't help that I don't have any log info from the server side. But I have tried...
  • Sending the request headers within the HTTPCLient command -

    Code: Select all

    AT+HTTPCLIENT=3,1,"https://test1-dec1.restdb.io/rest/pumpdata",,,2,"\"speed\": \"20\"","\"x-apikey\": \"919f6959b41e9a573fdfa4181b783cbf7bd19\"","\"cache-control\": \"no-cache\""
    • Sending the request headers with the HTTPCHEAD command then sending the GET or POST separately --

      Code: Select all

      AT+HTTPCHEAD=47
      	\"x-apikey\": \"919f6959b41e9a573fdfa4181b783cbf7bd19\"
      AT+HTTPCHEAD=27 
      	\"cache-control\": \"no-cache\"
      AT+HTTPCLIENT=2,1"https://test1-dec1.restdb.io/rest/pumpdata",,,2
    • sending as a HTTPCPOST command --

      Code: Select all

      AT+HTTPCPOST="https://test1-dec1.restdb.io/rest/pumpdata",110,3,"content-type: application/json","x-apikey: 919f6959b41e9a573fdfa4181b783cbf7bd19","cache-control: no-cache"
      


    I found the user guide confusing. And am new to HTTPS so that is making things difficult. Am i missing something? If anyone has any feedback or tips, that would be appreciated. Thanks!

esp-at
Espressif staff
Espressif staff
Posts: 335
Joined: Mon May 09, 2022 3:00 am

Re: Struggling with HTTPS request header on POST to REST API database

Postby esp-at » Fri Mar 21, 2025 4:08 am

Hi, could you please use `X-API-Key` instead of `x-apikey` in your AT commands:

Code: Select all

AT+HTTPCLIENT=3,1,"https://test1-dec1.restdb.io/rest/pumpdata",,,2,"\"speed\": \"20\"","\"x-apikey\": \"919f6959b41e9a573fdfa4181b783cbf7bd19\"","\"cache-control\": \"no-cache\""
according to:
https://docs.aws.amazon.com/apigateway/ ... ource.html

In general, there is nothing wrong with your AT command. As described in the example:
https://docs.espressif.com/projects/esp ... nt-of-data

patricia
Posts: 2
Joined: Tue Mar 18, 2025 11:35 pm

Re: Struggling with HTTPS request header on POST to REST API database

Postby patricia » Tue Apr 08, 2025 8:47 pm

No luck with capitalizing 'X-API-Key'. The ESP32 still returns "ERROR"

I installed ESP-IDF and am now able to locally compile the ESP-AT project. I am trying to enable more debug logs to get a better understanding of why the "ERROR" message is being printed. I followed the steps in the attached image. It resulted in what I think to be some debug logs on the "USB to UART bridge" COM port(image also attached)

Do you have any suggestions on how to glean more information from the firmware about the cause of the ERROR message? Thank you!

esp-at
Espressif staff
Espressif staff
Posts: 335
Joined: Mon May 09, 2022 3:00 am

Re: Struggling with HTTPS request header on POST to REST API database

Postby esp-at » Mon Apr 14, 2025 3:26 am

yes, you can see more details after you enabled the debug log:
python build.py menuconfig > Component config > Log output > Debug

The error prompt should be that http status code is not 200. Maybe you can compare the http request between esp32 and pc.

btw, you can use curl tool to perform post request, and you see the more details by the parameter of curl.

akshay9
Posts: 5
Joined: Thu Jan 09, 2025 9:32 pm

Re: Struggling with HTTPS request header on POST to REST API database

Postby akshay9 » Wed Apr 16, 2025 10:42 pm

Hi patricia,

We have a lite version of our product using ESP32 - C2/C3/C6/S2/S3/ESP32 to communicate with rest api end points. The code resides on ESP32 and assumes as the main processor. You can download it from https://github.com/OTAINFO/Downloads/tree/main/ESP-IDF.

Happy to help in your usecase.

Best,
Akshay

esp-at
Espressif staff
Espressif staff
Posts: 335
Joined: Mon May 09, 2022 3:00 am

Re: Struggling with HTTPS request header on POST to REST API database

Postby esp-at » Thu Apr 17, 2025 3:14 am

......

Who is online

Users browsing this forum: No registered users and 1 guest