HTTPClient error codes
HTTPClient::GET(), POST() and sendRequest() return an int: a negative HTTPC_ERROR_* code when the request failed on the ESP32 side, or the HTTP status code the server sent.
In short
- Negative return value: HTTPClient's own error, -1…-11. Positive: the status code from the server's response line (200, 404, 500…).1
HTTPClient::errorToString(code)gives the text for -1…-11 and an empty String for any other value.2- -1 "connection refused" is returned for any failed connect:
begin()missing or failed, or the client'sconnect()to host:port failing or timing out. The connect and read timeouts both default to 5000 ms.1 - On a negative code HTTPClient logs "error(<code>): <text>" as a warning and closes the connection.2
Error codes
| Code | Constant | errorToString() | Where it comes from |
|---|---|---|---|
-1 | HTTPC_ERROR_CONNECTION_REFUSED | connection refused | connect() failed: begin() was not called or failed, or the client's connect() to host:port did not succeed within the connect timeout. Not only a refused TCP connection.1 |
-2 | HTTPC_ERROR_SEND_HEADER_FAILED | send header failed | Writing the request header failed (or the connection was already gone).1 |
-3 | HTTPC_ERROR_SEND_PAYLOAD_FAILED | send payload failed | Not all of the request body could be written (for a buffer payload, one retry after 100 ms).1 |
-4 | HTTPC_ERROR_NOT_CONNECTED | not connected | No open connection when reading the response or the body.1 |
-5 | HTTPC_ERROR_CONNECTION_LOST | connection lost | The connection closed before the response header was complete, or while reading a chunked body.1 |
-6 | HTTPC_ERROR_NO_STREAM | no stream | A null Stream pointer was passed to sendRequest() or writeToStream().1 |
-7 | HTTPC_ERROR_NO_HTTP_SERVER | no HTTP server | The response header ended without a status code in the first line.1 |
-8 | HTTPC_ERROR_TOO_LESS_RAM | too less ram | The transfer buffer could not be allocated (stream upload or writeToStream()).1 |
-9 | HTTPC_ERROR_ENCODING | Transfer-Encoding not supported | The server sent a Transfer-Encoding other than chunked or identity.1 |
-10 | HTTPC_ERROR_STREAM_WRITE | Stream write error | Writing the body to the target Stream failed or wrote fewer bytes than expected.1 |
-11 | HTTPC_ERROR_READ_TIMEOUT | read Timeout | No data from the server for longer than the read timeout while waiting for the response header (also on a broken chunked body).1 |
Positive values: HTTP status codes
HTTPClient.h names the HTTP status codes as HTTP_CODE_* constants. The most common ones:
Checking the result
Timeouts
setConnectTimeout(ms) sets the connect timeout, setTimeout(ms) the read timeout (a uint16_t, so at most 65535 ms). Both start at 5000 ms.1
HTTPClient http;
http.begin("http://example.com/");
int code = http.GET();
if (code > 0) {
Serial.printf("HTTP status %d\n", code);
} else {
Serial.printf("HTTPClient error %d: %s\n", code, HTTPClient::errorToString(code).c_str());
}
http.end();Pitfalls
Redirects are not followed by default
A 301 or 302 comes back as the return value. Call setFollowRedirects() to make HTTPClient follow them (up to 10 by default).1
getString() hides errors
getString() ignores the error from writeToStream(): if reading fails part-way it returns what was read so far (truncated or empty), and an empty String if the buffer could not be reserved. Check the GET() result first.2
See also
Sources
Each number links here; open “What it confirms” for the exact quotes. Checked 2026-11-28.
- arduino-esp32 3.3.12: libraries/HTTPClient/src/HTTPClient.h primary
What it confirms (25)
- HTTPClient error codes: in short “#define HTTPC_ERROR_CONNECTION_REFUSED (-1) … #define HTTPC_ERROR_READ_TIMEOUT (-11) … _returnCode = headerLine.substring(codePos, headerLine.indexOf(' ', codePos)).toInt(); … if (_returnCode) { return _returnCode;”
- HTTPClient error codes: in short “#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000) … int32_t _connectTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; … uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; … if ((millis() - lastDataTime) > _tcpTimeout) { … if (!_client->connect(_host.c_str(), _port, _connectTimeout)) {”
- HTTPClient error codes: -1 “#define HTTPC_ERROR_CONNECTION_REFUSED (-1) … case HTTPC_ERROR_CONNECTION_REFUSED: … return F("connection refused"); … if (!connect()) { return returnError(HTTPC_ERROR_CONNECTION_REFUSED); … log_d("HTTPClient::begin was not called or returned error"); … if (!_client->connect(_host.c_str(), _port, _connectTimeout)) {”
- HTTPClient error codes: -2 “#define HTTPC_ERROR_SEND_HEADER_FAILED (-2) … case HTTPC_ERROR_SEND_HEADER_FAILED: … return F("send header failed"); … if (!sendHeader(type)) { return returnError(HTTPC_ERROR_SEND_HEADER_FAILED); … return (_client->write((const uint8_t *)header.c_str(), header.length()) == header.length()); … bool HTTPClient::sendHeader(const char *type) { if (!connected()) { return false;”
- HTTPClient error codes: -3 “#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3) … case HTTPC_ERROR_SEND_PAYLOAD_FAILED: … return F("send payload failed"); … if (sent_bytes != size) { return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); … log_w("Failed to send chunk! Lets wait a bit"); delay(100);”
- HTTPClient error codes: -4 “#define HTTPC_ERROR_NOT_CONNECTED (-4) … case HTTPC_ERROR_NOT_CONNECTED: … return F("not connected"); … if (!connected()) { return HTTPC_ERROR_NOT_CONNECTED; … if (!connected()) { return returnError(HTTPC_ERROR_NOT_CONNECTED);”
- HTTPClient error codes: -5 “#define HTTPC_ERROR_CONNECTION_LOST (-5) … case HTTPC_ERROR_CONNECTION_LOST: … return F("connection lost"); … return HTTPC_ERROR_CONNECTION_LOST; } … if (!connected()) { return returnError(HTTPC_ERROR_CONNECTION_LOST);”
- HTTPClient error codes: -6 “#define HTTPC_ERROR_NO_STREAM (-6) … case HTTPC_ERROR_NO_STREAM: … return F("no stream"); … if (!stream) { return returnError(HTTPC_ERROR_NO_STREAM);”
- HTTPClient error codes: -7 “#define HTTPC_ERROR_NO_HTTP_SERVER (-7) … case HTTPC_ERROR_NO_HTTP_SERVER: … return F("no HTTP server"); … log_d("Remote host is not an HTTP Server!"); return HTTPC_ERROR_NO_HTTP_SERVER;”
- HTTPClient error codes: -8 “#define HTTPC_ERROR_TOO_LESS_RAM (-8) … case HTTPC_ERROR_TOO_LESS_RAM: … return F("too less ram"); … log_d("too less ram! need %d", buff_size); return returnError(HTTPC_ERROR_TOO_LESS_RAM); … log_w("too less ram! need %d", buff_size); return HTTPC_ERROR_TOO_LESS_RAM;”
- HTTPClient error codes: -9 “#define HTTPC_ERROR_ENCODING (-9) … case HTTPC_ERROR_ENCODING: … return F("Transfer-Encoding not supported"); … if (transferEncoding.equalsIgnoreCase("chunked")) { … } else if (transferEncoding.equalsIgnoreCase("identity")) { _transferEncoding = HTTPC_TE_IDENTITY; } else { return HTTPC_ERROR_ENCODING;”
- HTTPClient error codes: -10 “#define HTTPC_ERROR_STREAM_WRITE (-10) … case HTTPC_ERROR_STREAM_WRITE: … return F("Stream write error"); … if (stream->getWriteError()) { … return HTTPC_ERROR_STREAM_WRITE; … if (ret != _size) { return returnError(HTTPC_ERROR_STREAM_WRITE);”
- HTTPClient error codes: -11 “#define HTTPC_ERROR_READ_TIMEOUT (-11) … case HTTPC_ERROR_READ_TIMEOUT: … return F("read Timeout"); … if ((millis() - lastDataTime) > _tcpTimeout) { return HTTPC_ERROR_READ_TIMEOUT; … if (chunkHeader.length() <= 0) { return returnError(HTTPC_ERROR_READ_TIMEOUT);”
- HTTPClient error codes: 200 “HTTP_CODE_OK = 200,”
- HTTPClient error codes: 301 “HTTP_CODE_MOVED_PERMANENTLY = 301,”
- HTTPClient error codes: 302 “HTTP_CODE_FOUND = 302,”
- HTTPClient error codes: 304 “HTTP_CODE_NOT_MODIFIED = 304,”
- HTTPClient error codes: 400 “HTTP_CODE_BAD_REQUEST = 400,”
- HTTPClient error codes: 401 “HTTP_CODE_UNAUTHORIZED = 401,”
- HTTPClient error codes: 403 “HTTP_CODE_FORBIDDEN = 403,”
- HTTPClient error codes: 404 “HTTP_CODE_NOT_FOUND = 404,”
- HTTPClient error codes: 500 “HTTP_CODE_INTERNAL_SERVER_ERROR = 500,”
- HTTPClient error codes: 503 “HTTP_CODE_SERVICE_UNAVAILABLE = 503,”
- HTTPClient error codes: Timeouts “void setConnectTimeout(int32_t connectTimeout); … void setTimeout(uint16_t timeout); … #define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000) … if ((millis() - lastDataTime) > _tcpTimeout) {”
- HTTPClient error codes: pitfall: Redirects are not followed by default “followRedirects_t _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS; … `HTTPC_DISABLE_FOLLOW_REDIRECTS` - no redirection will be followed. … uint16_t _redirectLimit = 10;”
- arduino-esp32 3.3.12: libraries/HTTPClient/src/HTTPClient.cpp primary
What it confirms (3)
- HTTPClient error codes: in short “String HTTPClient::errorToString(int error) { … default: return String();”
- HTTPClient error codes: in short “if (error < 0) { log_w("error(%d): %s", error, errorToString(error).c_str()); if (connected()) { log_d("tcp stop"); _client->stop();”
- HTTPClient error codes: pitfall: getString() hides errors “if (sstring.reserve((_size + 1))) { writeToStream(&sstring); return sstring; … log_d("not enough memory to reserve a string! need: %d", (_size + 1));”