-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Comments
You need to set collectHeaders like explaned here. |
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");
}
} |
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. |
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. |
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. |
Using |
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? |
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
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: