ESPHome 2026.2.1
Loading...
Searching...
No Matches
esphome::http_request::HttpContainer Class Referenceabstract

#include <http_request.h>

Inheritance diagram for esphome::http_request::HttpContainer:
esphome::Parented< HttpRequestComponent > esphome::http_request::HttpContainerArduino esphome::http_request::HttpContainerHost esphome::http_request::HttpContainerIDF

Public Member Functions

virtual ~HttpContainer ()=default
 
virtual int read (uint8_t *buf, size_t max_len)=0
 Read data from the HTTP response body.
 
virtual void end ()=0
 
void set_secure (bool secure)
 
void set_chunked (bool chunked)
 
size_t get_bytes_read () const
 
virtual bool is_read_complete () const
 Check if all expected content has been read.
 
std::map< std::string, std::list< std::string > > get_response_headers ()
 Get response headers.
 
std::string get_response_header (const std::string &header_name)
 
- Public Member Functions inherited from esphome::Parented< HttpRequestComponent >
 Parented ()
 
 Parented (HttpRequestComponent *parent)
 
HttpRequestComponent * get_parent () const
 Get the parent of this object.
 
void set_parent (HttpRequestComponent *parent)
 Set the parent of this object.
 

Data Fields

size_t content_length {0}
 
int status_code {-1}
 -1 indicates no response received yet
 
uint32_t duration_ms {0}
 

Protected Attributes

size_t bytes_read_ {0}
 
bool secure_ {false}
 
bool is_chunked_ {false}
 True if response uses chunked transfer encoding.
 
std::map< std::string, std::list< std::string > > response_headers_ {}
 
- Protected Attributes inherited from esphome::Parented< HttpRequestComponent >
HttpRequestComponent * parent_
 

Detailed Description

Definition at line 201 of file http_request.h.

Constructor & Destructor Documentation

◆ ~HttpContainer()

virtual esphome::http_request::HttpContainer::~HttpContainer ( )
virtualdefault

Member Function Documentation

◆ end()

virtual void esphome::http_request::HttpContainer::end ( )
pure virtual

◆ get_bytes_read()

size_t esphome::http_request::HttpContainer::get_bytes_read ( ) const
inline

Definition at line 241 of file http_request.h.

◆ get_response_header()

std::string esphome::http_request::HttpContainer::get_response_header ( const std::string & header_name)

Definition at line 24 of file http_request.cpp.

◆ get_response_headers()

std::map< std::string, std::list< std::string > > esphome::http_request::HttpContainer::get_response_headers ( )
inline

Get response headers.

Returns
The key is the lower case response header name, the value is the header value.

Definition at line 266 of file http_request.h.

◆ is_read_complete()

virtual bool esphome::http_request::HttpContainer::is_read_complete ( ) const
inlinevirtual

Check if all expected content has been read.

Base implementation handles non-chunked responses and status-code-based no-body checks. Platform implementations may override for chunked completion detection:

  • ESP-IDF: overrides to call esp_http_client_is_complete_data_received() for chunked.
  • Arduino: read_chunked_() clears is_chunked_ and sets content_length on the final chunk, after which the base implementation detects completion.

Reimplemented in esphome::http_request::HttpContainerIDF.

Definition at line 249 of file http_request.h.

◆ read()

virtual int esphome::http_request::HttpContainer::read ( uint8_t * buf,
size_t max_len )
pure virtual

Read data from the HTTP response body.

WARNING: These semantics differ from BSD sockets! BSD sockets: 0 = EOF (connection closed) This method: 0 = no data yet OR all content read, negative = error/closed

Parameters
bufBuffer to read data into
max_lenMaximum number of bytes to read
Returns
  • > 0: Number of bytes read successfully
  • 0: No data available yet OR all content read (check get_bytes_read() >= content_length to distinguish)
  • HTTP_ERROR_CONNECTION_CLOSED (-1): Connection closed prematurely
  • < -1: Other error (platform-specific error code)

Platform notes:

  • ESP-IDF: blocking read, 0 only when all content read
  • Arduino: non-blocking, 0 can mean "no data yet" or "all content read"

Use get_bytes_read() and content_length to track progress. When get_bytes_read() >= content_length, all data has been received.

IMPORTANT: Do not use raw return values directly. Use these helpers:

Implemented in esphome::http_request::HttpContainerArduino, esphome::http_request::HttpContainerHost, and esphome::http_request::HttpContainerIDF.

◆ set_chunked()

void esphome::http_request::HttpContainer::set_chunked ( bool chunked)
inline

Definition at line 239 of file http_request.h.

◆ set_secure()

void esphome::http_request::HttpContainer::set_secure ( bool secure)
inline

Definition at line 238 of file http_request.h.

Field Documentation

◆ bytes_read_

size_t esphome::http_request::HttpContainer::bytes_read_ {0}
protected

Definition at line 271 of file http_request.h.

◆ content_length

size_t esphome::http_request::HttpContainer::content_length {0}

Definition at line 204 of file http_request.h.

◆ duration_ms

uint32_t esphome::http_request::HttpContainer::duration_ms {0}

Definition at line 206 of file http_request.h.

◆ is_chunked_

bool esphome::http_request::HttpContainer::is_chunked_ {false}
protected

True if response uses chunked transfer encoding.

Definition at line 273 of file http_request.h.

◆ response_headers_

std::map<std::string, std::list<std::string> > esphome::http_request::HttpContainer::response_headers_ {}
protected

Definition at line 274 of file http_request.h.

◆ secure_

bool esphome::http_request::HttpContainer::secure_ {false}
protected

Definition at line 272 of file http_request.h.

◆ status_code

int esphome::http_request::HttpContainer::status_code {-1}

-1 indicates no response received yet

Definition at line 205 of file http_request.h.


The documentation for this class was generated from the following files: