I’m trying to perform a POST operation from a web client application running on an ESP32-WROOM-32 to a web server running on Windows 11 using Apache. On the backend I’m trying to use JavaScript to handle the received data (JSON string). The problem is that I can’t see/receive the JSON string sent by the ESP32, although on the VS Code terminal(from the ESP32 side) I get:
I (79753440) HTTP_CLIENT: HTTP POST Status = 200, content_length = 334
I (79753440) HTTP_CLIENT: HTTP_EVENT_DISCONNECTED
So, I assume the data has been successfuly sent. I’m using the esp_http_client example.
I have already
1. Activated the reverse proxy on Apache using the mod_proxy module:
ProxyPass /post http://192.168.0.101:8000/
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
2. Written JavaScript and html codes to handle the sent data(I’m really a newbie in js/html):
const intervalID = setInterval(get_user_ID,1000);
function get_user_ID()
{
var xhr = new XMLHttpRequest();
var requestURL = "/post";
xhr.open('GET', requestURL);
xhr.send();
if (xhr.readyState == 4 && xhr.status == 200)
{
var response = JSON.parse(xhr.responseText);
console.log(response);
document.getElementById("esp_response").innerHTML = response.User;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script async src='serverJS.js'></script>
</head>
<body>
<h1>ESP32 get data</h1>
<label id="esp_response_label">ESP32 response: </label>
<div id="esp_response"></div>
</body>
</html>
As a result, my html labels are printed correctly but I still get no data on the web browser console. Tried out the fetch API but no success. In one of the approaches I was getting response = null on the console. What could I be missging?
Just in case, I leave my esp_http_client piece of code as well:
#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);
}