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.
esp_http_client POST'ing a null pointer
-
- Posts: 1735
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: esp_http_client POST'ing a null pointer
So you're sending a POST request with a "user id" from the ESP to the webserver.
And you send the same POST request, but without any data, to the same webserver/URL from JavaScript.
This is either not what you want, or you have/need some code (PHP,...) on the webserver to accept a "user id" (from the ESP), store it somewhere, and retrieve&return the stored data when another POST request (from JavaScript) arrives.
Conventionally, you want to use POST to send data from a client to a webserver, and use GET to retrieve data from the webserver.
And you send the same POST request, but without any data, to the same webserver/URL from JavaScript.
This is either not what you want, or you have/need some code (PHP,...) on the webserver to accept a "user id" (from the ESP), store it somewhere, and retrieve&return the stored data when another POST request (from JavaScript) arrives.
Conventionally, you want to use POST to send data from a client to a webserver, and use GET to retrieve data from the webserver.
-
- Posts: 15
- Joined: Fri Mar 19, 2021 9:58 pm
Re: esp_http_client POST'ing a null pointer
My intention was to print the data on a second client(the web page) , assuming that the sent user id was forwarded on the background to my javascript code. I'm a newbie in js, so I tried xhr.open('GET', requestURL); but it didn't work as well.
So, are you trying to say that the javascript code is not what I need to receive and store the user id on the web server? I need some node.js / PHP code?you have/need some code (PHP,...) on the webserver to accept a "user id"
-
- Posts: 15
- Joined: Fri Mar 19, 2021 9:58 pm
Re: esp_http_client POST'ing a null pointer
I finally figure out how to solve this issue: I have deleted apache and my JavaScript code and written a code in node.js to do the job. After some modifications in my ESP32 code I got it to work. Thank you for the hint.
Who is online
Users browsing this forum: No registered users and 135 guests