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.

Activity

Jeroen88

Jeroen88 commented on May 3, 2025

@Jeroen88
Contributor

You need to set collectHeaders like explaned here.

sr5434

sr5434 commented on May 3, 2025

@sr5434
Author

I just tried that, but unfortunately that does not seem to work either. Here is the code I used:

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);
    const char* headerNames[] = { "Location", "Last-Modified" };
    http.collectHeaders(headerNames, sizeof(headerNames)/sizeof(headerNames[0]));
    // 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");
  }
}
lbernstone

lbernstone commented on May 3, 2025

@lbernstone
Contributor

Is what you really want here to have 308 (HTTP_CODE_PERMANENT_REDIRECT) behave like the other redirects? Try changing sendRequest and see if that gives you the behavior you want.

sr5434

sr5434 commented on May 4, 2025

@sr5434
Author

My issue is not with following redirects, but that redirects are necessary in the first place. When I use curl, I am not being redirected, but on my ESP32, I am redirected to a blank URL.

lbernstone

lbernstone commented on May 4, 2025

@lbernstone
Contributor

Run curl with -vvv options to see the full transaction. Turn on verbose logging on the esp32 to see the same transaction. Make sure that the server response is the same for both. The client is not driving this- if there is a difference, that is coming from the server.

sr5434

sr5434 commented on May 4, 2025

@sr5434
Author

Using esp_log.h and running esp_log_level_set("*", ESP_LOG_VERBOSE); in setup does not print anything. Am I doing something wrong?

lbernstone

lbernstone commented on May 4, 2025

@lbernstone
Contributor

Tools menu

Image

sr5434

sr5434 commented on May 5, 2025

@sr5434
Author

I noticed that in the logs on my ESP32 it said that it received an HTTP1.0 response but in CuRL it said it was running HTTP 2.0. Could that be the reason why I am getting redirected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lbernstone@Jeroen88@sr5434

        Issue actions

          308 Redirect when Making POST Request, Despite Curl Working · Issue #11333 · espressif/arduino-esp32