采用 esp_http_client_example 例程
采用 static void http_native_request(void)
不直接用POST ,必须从GET开始
如果我把
在POST 加入
esp_http_client_fetch_headers(esp_http_client_handle_t client),不在之前直接
client->state= HTTP_STATE_RES_COMPLETE_HEADER 会不会有问题 ,要如何解决?
HTTP 问题
Re: HTTP 问题
不是很明白你要表达的意思,你可以直接贴代码或者重新整理下你想要咨询的问题。
Re: HTTP 问题
只用 esp_http_client案例中 http_native_request ,将GET 部分 隐去,将esp_http_client_fetch_headers(client)
加到POST 进程中,执行出现"HTTP client fetch headers failed"
static void http_native_request(void)
{
char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; // Buffer to store response of http request
int content_length = 0;
esp_http_client_config_t config = {
.url = "http://httpbin.org/get",
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// GET Request
// esp_http_client_set_method(client, HTTP_METHOD_GET);
// esp_err_t err = esp_http_client_open(client, 0);
// if (err != ESP_OK) {
// ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
// } else {
// content_length = esp_http_client_fetch_headers(client);
// if (content_length < 0) {
// ESP_LOGE(TAG, "HTTP client fetch headers failed");
// } else {
// int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
// if (data_read >= 0) {
// ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
// esp_http_client_get_status_code(client),
// esp_http_client_get_content_length(client));
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
// } else {
// ESP_LOGE(TAG, "Failed to read response");
// }
// }
// }
// esp_http_client_close(client);
// POST Request
const char *post_data = "{\"field1\":\"value1\"}";
esp_http_client_set_url(client, "http://httpbin.org/post");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_err_t err = esp_http_client_open(client, strlen(post_data));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
} else {
content_length = esp_http_client_fetch_headers(client);
if (content_length < 0) {
ESP_LOGE(TAG, "HTTP client fetch headers failed");
} else {
int wlen = esp_http_client_write(client, post_data, strlen(post_data));
if (wlen < 0) {
ESP_LOGE(TAG, "Write failed");
}
int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
if (data_read >= 0) {
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
} else {
ESP_LOGE(TAG, "Failed to read response");
}
}
}
esp_http_client_cleanup(client);
}
加到POST 进程中,执行出现"HTTP client fetch headers failed"
static void http_native_request(void)
{
char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; // Buffer to store response of http request
int content_length = 0;
esp_http_client_config_t config = {
.url = "http://httpbin.org/get",
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// GET Request
// esp_http_client_set_method(client, HTTP_METHOD_GET);
// esp_err_t err = esp_http_client_open(client, 0);
// if (err != ESP_OK) {
// ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
// } else {
// content_length = esp_http_client_fetch_headers(client);
// if (content_length < 0) {
// ESP_LOGE(TAG, "HTTP client fetch headers failed");
// } else {
// int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
// if (data_read >= 0) {
// ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
// esp_http_client_get_status_code(client),
// esp_http_client_get_content_length(client));
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
// } else {
// ESP_LOGE(TAG, "Failed to read response");
// }
// }
// }
// esp_http_client_close(client);
// POST Request
const char *post_data = "{\"field1\":\"value1\"}";
esp_http_client_set_url(client, "http://httpbin.org/post");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_err_t err = esp_http_client_open(client, strlen(post_data));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
} else {
content_length = esp_http_client_fetch_headers(client);
if (content_length < 0) {
ESP_LOGE(TAG, "HTTP client fetch headers failed");
} else {
int wlen = esp_http_client_write(client, post_data, strlen(post_data));
if (wlen < 0) {
ESP_LOGE(TAG, "Write failed");
}
int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
if (data_read >= 0) {
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
} else {
ESP_LOGE(TAG, "Failed to read response");
}
}
}
esp_http_client_cleanup(client);
}
Re: HTTP 问题
你需要分清 HTTP GET 和 POST 请求的差别。当我们使用 GET 请求时,我们需要发送 HTTP 头并带上 GET 请求(esp_http_client_open),此时对端会把我们请求的数据发送回来,这个时候我们调用 esp_http_client_fetch_headers 是可以读到 HTTP 头数据的。但是当我们是 POST 请求时,esp_http_client_open 只是发送了HTTP 头并带上 POST 请求,并没有发送 POST 数据,服务器是不会回的, POST 数据是后续函数 esp_http_client_write 发给服务器的。你可以把 esp_http_client_fetch_headers 放到 esp_http_client_write 之后。
Who is online
Users browsing this forum: No registered users and 149 guests