Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
sr5434 opened this issue May 2, 2025 · 8 comments
Open
1 task done

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

sr5434 opened this issue May 2, 2025 · 8 comments
Labels
Status: Awaiting triage Issue is waiting for triage

Comments

@sr5434
Copy link

sr5434 commented May 2, 2025

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.
@sr5434 sr5434 added the Status: Awaiting triage Issue is waiting for triage label May 2, 2025
@Jeroen88
Copy link
Contributor

Jeroen88 commented May 3, 2025

You need to set collectHeaders like explaned here.

@sr5434
Copy link
Author

sr5434 commented May 3, 2025

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
Copy link
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
Copy link
Author

sr5434 commented May 4, 2025

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
Copy link
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
Copy link
Author

sr5434 commented May 4, 2025

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
Copy link
Contributor

Tools menu

Image

@sr5434
Copy link
Author

sr5434 commented May 5, 2025

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
Labels
Status: Awaiting triage Issue is waiting for triage
Projects
None yet
Development

No branches or pull requests

3 participants