Skip to content

[HELP] How to post an HTTP with headers? #3483

Closed
@sarkrui

Description

@sarkrui

Hardware:

Board: Wemos Lolin32 Lite
Core Installation version: 1.0.4
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: Unknown
Upload Speed: 115200
Computer OS: Mac OSX

Description:

I was able to POST a header-less HTTP request to Firebase Realtime Database, while I failed to post HTTP requests with headers (http code 403.) to an Entity Dataset powered by TU Eindhoven. The server side seems to work well as I was able to POST, GET, PUT via Insomnia. On my ESP32, the http.getString returns Access with api_token failed., which seems headers of the request were missing (it's confirmed from the server side that there were attempts of HTTP requests without headers, although I did include them in the code).

Sketch: (leave the backquotes for code formatting)

//Change the code below by your sketch
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v6
const char* ssid     = "";
const char* password = "";
int sensor_0 = 1234;
int sensor_1 = 5678;
String postMessage;

void setup() {
void WPAConnect() {

  Serial.begin(115200);
  delay(500);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WPAPersonal Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(200);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
}

void setup() {
  WPAConnect();
  curlRequest();
}
void curlRequest() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    HTTPClient http;
    const int capacity = JSON_OBJECT_SIZE(2);
    StaticJsonDocument<capacity> doc;
    doc["sensor_0"] = sensor_0;
    doc["sensor_1"] = sensor_1;
    serializeJsonPretty(doc, postMessage);
    serializeJsonPretty(doc, Serial);
    http.begin("https://data.id.tue.nl/datasets/entity/145/item/"); //Specify destination for HTTP request
    http.addHeader("Content-Type", "application/json");
    http.addHeader("api_token",  "pMXFOLpinNq7EzrVQ/nXXLWl+96c9Dk+f8K2iVj+QoQqajaRQJYMeWObg2XYmcX1");
    http.addHeader("resource_id", "breathing_0");
    http.addHeader("token", "1234");
    // http.begin("https://tu-eindhoven-shared-projects.firebaseio.com/.json"); //Specify destination for HTTP request
    int httpCode = http.POST(postMessage);
    if (httpCode > 0) {
      Serial.println();
      Serial.println(httpCode);
      if (httpCode == 200) {
        Serial.println("Hooray!");
      }
    }
  }
}
void loop() {
}

Debug Messages:

{
  "sensor_0": 1234,
  "sensor_1": 5678
}Access with api_token failed.
403

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions