Skip to content

Bad HTTP response #11375

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
ITstreet1 opened this issue May 17, 2025 · 2 comments
Open
1 task done

Bad HTTP response #11375

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

Comments

@ITstreet1
Copy link

Board

ESP32 C3

Device Description

Devkit

Hardware Configuration

Nothing

Version

latest stable Release (if not listed below)

IDE Name

Arduino IDE

Operating System

Win10

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

I tried to use my older code but always get a bad HTTP response. Can someone point me what to change?
The URL is ok, as I get the valid data over the browser with HTTP code 200.

Sketch

#include <WiFi.h>
#include <HTTPClient.h>
WiFiClient client;

const char* ssid = "SSID";
const char* password = "PASS";
const int deviceID = 2;
String server = "www.somesite.com";

void setup(){
  Serial.begin(9600);
  Serial.println();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while((WiFi.status() != WL_CONNECTED)) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("Connected!");

}
void loop(){
  Serial.println("test");
  if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;
      String serverPath = server;
      Serial.println(serverPath);
      Serial.println("-----");
      http.begin(client, serverPath.c_str());
      int httpResponseCode = http.GET();
      if(httpResponseCode>0){
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
      }else{
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      http.end();
    }else{
      Serial.println("WiFi Disconnected");
    }
  delay(5000);
}

Debug Message

I always get -1 as HTTP code. Verbose output this:

[  6031][V][HTTPClient.cpp:259] beginInternal(): url: www.somesite.com
[  6315][D][HTTPClient.cpp:314] beginInternal(): protocol: http, host: www.somesite.com port: 80 url: ....//link is not important
[  6505][D][HTTPClient.cpp:621] sendRequest(): request type: 'GET' redirCount: 0

[  6593][D][NetworkManager.cpp:83] hostByName(): Clearing DNS cache
[  6670][D][NetworkManager.cpp:127] hostByName(): DNS found IPv4 *****
[  6776][E][NetworkClient.cpp:293] connect(): Setsockopt 'SO_SNDTIMEO'' on fd 48 failed. errno: 22, "Invalid argument"
[  6901][D][HTTPClient.cpp:1248] connect(): failed connect to www.somesite.com:80
[  6983][W][HTTPClient.cpp:1604] returnError(): error(-1): connection refused
Error code: -1
[  7066][D][HTTPClient.cpp:434] disconnect(): tcp is closed

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.
@ITstreet1 ITstreet1 added the Status: Awaiting triage Issue is waiting for triage label May 17, 2025
@ITstreet1
Copy link
Author

HTTPClient.h line 47 clearly says #define HTTPC_ERROR_CONNECTION_REFUSED (-1), which is what I get on the output, but why? What is changed in v3.x.x? What should be changed in begin to work properly?

From what I have seen, it all started from V3.x.x. There are a bunch of similar problems on the internet.

@lbernstone
Copy link
Contributor

I'd recommend you use curl -v <url> or your browser's network tools to see what the transaction looks like when it completes. There is likely a redirect there to https or perhaps http 2.0. You could also check the verbose log of an earlier version of arduino-esp32 to see what the server response looks like.

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

2 participants