Sending a request to https server.
Posted: Tue Nov 06, 2018 2:11 pm
I an trying to download the content of a webpage and I get the response with 307 status code with "a href="/..." message.
The response I got looks like this: HTTP/1.0 307 Temporary Redirect Content-Length: 123 Content-Type: text/html; charset=utf-8 Date: Tue, 06 Nov 2018 09:44:40 GMT Location: "<a href="/oauth/authorize?state=L2FwaS9wcm...=">Temporary Redirect</a>"
I use esp_http_client example and https() function.
The function: esp_http_client_fetch_headers(client) returns -1, and when I comment
this line: if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
and in call this function: esp_http_client_read(client, buffer, content_length) I put e.g 512 instead "content_length" I get response with 307 status code.
The response I got looks like this: HTTP/1.0 307 Temporary Redirect Content-Length: 123 Content-Type: text/html; charset=utf-8 Date: Tue, 06 Nov 2018 09:44:40 GMT Location: "<a href="/oauth/authorize?state=L2FwaS9wcm...=">Temporary Redirect</a>"
I use esp_http_client example and https() function.
The function: esp_http_client_fetch_headers(client) returns -1, and when I comment
this line: if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
and in call this function: esp_http_client_read(client, buffer, content_length) I put e.g 512 instead "content_length" I get response with 307 status code.
Code: Select all
static void https()
{
esp_http_client_config_t config = {
.url = "any URL",
.host = " any host ",
.port = 443,
.username = "username", /*!< Using for Http authentication */
.password = "password", /*!< Using for Http authentication */
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1);
int content_length = esp_http_client_fetch_headers(client);
ESP_LOGW( "TEST content_length", "%d", content_length);
int total_read_len = 0, read_len;
if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
read_len = esp_http_client_read(client, buffer, content_length);
if (read_len <= 0) {
ESP_LOGE("HTTPS", "Error read data");
}
ESP_LOGW( "HTTPS response: ", "%s", buffer);
}
esp_http_client_cleanup(client);
}