esp_http_client POST'ing a null pointer
Posted: Thu Aug 08, 2024 10:21 am
Hello,
I'm trying to perform a POST operation from my ESP32-WROOM-32 running the esp_http_client example to a web server running on Windows 11 using apache. I wrote sobe JavaScript script to handle the JSON post_data in the backend and a basic html code to print it on the web page. The problem is that nothing is printed on the web page . When I open the browser console to debug the web application, a null is printed as POST response. May be someone can give some tips on how to solve this.
1. Here is the log from VS Code terminal
I (79753440) HTTP_CLIENT: HTTP POST Status = 200, content_length = 334
I (79753440) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
2. Here is my script
const intervalID = setInterval(get_user_ID,1000);
function get_user_ID()
{
var xhr = new XMLHttpRequest();
var requestURL = "http://192.168.0.101/post";
xhr.open('POST', requestURL);
xhr.send();
xhr.responseType = "json";
xhr.onload = () => {
if (xhr.readyState == 4 && xhr.status == 200)
{
console.log(xhr.response);
document.getElementById("esp_response").innerHTML = xhr.response.User;
}
};
}
3. The piece of code from the example(I'm running it on a dedicated task)
#define HTTP_ENDPOINT "192.168.0.101"
static void http_rest_with_url(void) {
esp_http_client_config_t config = {
.host = HTTP_ENDPOINT,
.path = "/",
.query = "esp",
.event_handler = _http_event_handler,
.user_data = local_response_buffer,
.disable_auto_redirect = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// POST
const char *post_data = "{\"User\":\"User1-1919\"}";
esp_http_client_set_url(client, "http://"HTTP_ENDPOINT"/post");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_post_field(client, post_data, strlen(post_data));
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
4. And finally the apache configuration (from the httpd.conf file)
ProxyPass /post http://192.168.0.101:8000/
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Looking forward for your answers and ready to give further clarifications.
I'm trying to perform a POST operation from my ESP32-WROOM-32 running the esp_http_client example to a web server running on Windows 11 using apache. I wrote sobe JavaScript script to handle the JSON post_data in the backend and a basic html code to print it on the web page. The problem is that nothing is printed on the web page . When I open the browser console to debug the web application, a null is printed as POST response. May be someone can give some tips on how to solve this.
1. Here is the log from VS Code terminal
I (79753440) HTTP_CLIENT: HTTP POST Status = 200, content_length = 334
I (79753440) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
2. Here is my script
const intervalID = setInterval(get_user_ID,1000);
function get_user_ID()
{
var xhr = new XMLHttpRequest();
var requestURL = "http://192.168.0.101/post";
xhr.open('POST', requestURL);
xhr.send();
xhr.responseType = "json";
xhr.onload = () => {
if (xhr.readyState == 4 && xhr.status == 200)
{
console.log(xhr.response);
document.getElementById("esp_response").innerHTML = xhr.response.User;
}
};
}
3. The piece of code from the example(I'm running it on a dedicated task)
#define HTTP_ENDPOINT "192.168.0.101"
static void http_rest_with_url(void) {
esp_http_client_config_t config = {
.host = HTTP_ENDPOINT,
.path = "/",
.query = "esp",
.event_handler = _http_event_handler,
.user_data = local_response_buffer,
.disable_auto_redirect = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
// POST
const char *post_data = "{\"User\":\"User1-1919\"}";
esp_http_client_set_url(client, "http://"HTTP_ENDPOINT"/post");
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_post_field(client, post_data, strlen(post_data));
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
4. And finally the apache configuration (from the httpd.conf file)
ProxyPass /post http://192.168.0.101:8000/
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Looking forward for your answers and ready to give further clarifications.