Skip to content

308 Redirect when Making POST Request, Despite Curl Working #11333

Open
@sr5434

Description

@sr5434

Board

ESP32-S v1.1

Device Description

Plain module on breadboard

Hardware Configuration

I have connected an LED to pin 12 and a soil moisture sensor to pin 36.

Version

v3.2.0

IDE Name

Arduino IDE

Operating System

macOS 15.4

Flash frequency

I am unsure

PSRAM enabled

no

Upload speed

9600

Description

I am trying to make an IoT Soil Moisture Monitor. When I make a POST request to my server from curl, the request goes through successfully. However, when I make the request through my ESP32, it keeps redirecting to an empty URL.

Sketch

#include <WiFi.h>
#include <HTTPClient.h>

#define SENSOR_INPUT 36//15
#define LIGHT_PORT 12

const String ssid = "REDACTED";
const String password = "REDACTED";
const String serverName = "https://soil-moisture-monitor-phi.vercel.app/api/updateMeasurement/";
int prevHumidity = 0;

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED){
    Serial.print("Connecting...");
    delay(100);
  }
  Serial.println("Connected!");
  // delay(1000);
  pinMode(LIGHT_PORT, OUTPUT);
  if(WiFi.status() == WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
  
    // Your Domain name with URL path or IP address with path
    http.begin(client, serverName);

    // Data to send with HTTP POST
    String payload = "{\"measurement\": "+String(12345) + "}";
    // Authorization
    // Send HTTP POST request
    http.addHeader("Content-Type", "application/json");
    // http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
    int httpResponseCode = http.POST(payload);
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    if (httpResponseCode == 308) {
      String location = http.header("Location");  // Get redirect target
      http.end();

      Serial.println("Redirected to: " + location);

      // Try the new location
      http.begin(client, location);
      http.addHeader("Content-Type", "application/json");
      httpResponseCode = http.POST(payload);
    }
    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
}

Debug Message

Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connecting...Connected!
17:39:00.373 -> HTTP Response code: 308
17:39:00.405 -> Redirected to:

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions