Page 1 of 1

esp32 http POST question

Posted: Sun Nov 10, 2024 2:37 am
by Gaston1980
Hi to all,
I'm using esp32 wroom kit to get familiar with the http protocol, try to use the POST request to send values to a server running on ubuntu machine. I'm able to connect to wifi router and using this code:

Code: Select all

    esp_http_client_config_t config_post = {
        .url = "http://192.168.0.4/index.php",
        .method = HTTP_METHOD_POST,
        .cert_pem = NULL,
        .event_handler = client_event_post_handler};
        
    esp_http_client_handle_t client = esp_http_client_init(&config_post);

    const char *post_data =  "from esp32";
    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));
    esp_err_t 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);
I'm getting this from the POST request:

Code: Select all

HTTP_EVENT_ON_DATA: 
<html>
<body>
 
</body>
</html>
In the php code I only print the value I got from the POST request....but there is nothing there...empty array....
I already test the php script with a little curl code and the php script is working.
Maybe I missed something in the config for the client but, shouldn't the POST request sent the array data? I don't understan why the php works with curl code and NOT with the POST request from the esp32.
I also tried with "/post" at the end of the url of the server but that didn't work either.
Regards
Gastón

Re: esp32 http POST question

Posted: Sun Nov 10, 2024 4:37 am
by ESP_Sprite
Can you show the php code? I notice you're not using form fields, so it may be that it doesn't properly parse what the ESP32 sends.

Re: esp32 http POST question

Posted: Sun Nov 10, 2024 1:54 pm
by Gaston1980
Hi ESP_Sprite,
yes the php code is just:

Code: Select all

<?php
    phpinfo(INFO_VARIABLES);
?>
I also tried this peace of code too:

Code: Select all

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  echo "<h1>Hello User, </h1> <p>Welcome to {$name}</p>";
}
?>
and many others php script, just to know if the POST request send something
form fileds in the esp32 code?
I notice also using the web debuuger and the php code is that the request method is:
Image

Shouldn't say "post"?
Maybe I missed something in the config.
Gastón

Re: esp32 http POST question

Posted: Sun Nov 10, 2024 11:58 pm
by Gaston1980
Hi, I found this thread in the forum https://esp32.com/viewtopic.php?t=7939 could this be related to Content-Lenght not been included in the headers?
Gastón

Re: esp32 http POST question

Posted: Mon Nov 11, 2024 2:43 am
by boarchuz
I think Sprite nailed it with the request body not matching the expected key=value format. There's also the JSON content type header which, at best, your server will ignore but may be messing things up too.

From a quick search you should be able to get the raw request body with file_get_contents('php://input').

Though I don't see how it would be possible for the method to be 'GET' in your php output there. Are you sure that's for the request that the ESP32 sent to the server, and not the one for fetching that page in your browser?

What status code and response does the ESP32 receive?

Re: esp32 http POST question

Posted: Mon Nov 11, 2024 10:10 am
by Gaston1980
yes, it was the json content-type. I tred to use the header Content-Lenght with the json and also with some random array but throws error.
Regards
Gastón

Re: esp32 http POST question

Posted: Wed Nov 13, 2024 3:22 am
by chegewara
How about this for start?

Code: Select all

<?php

  $json = file_get_contents('php://input'); 
  $data = json_decode($json);
  header('Content-Type: application/json; charset=utf-8');

  // Ensure $data is a valid JSON object
	if (json_last_error() === JSON_ERROR_NONE) {

		print_r($data);
	} else {
		echo "JSON error";	
	}
?>
This is valid if you want to send JSON to the server. In OP you mentioned about array tho, but you were sending string, which is not an array.

Re: esp32 http POST question

Posted: Wed Nov 13, 2024 1:24 pm
by Gaston1980
hi @chegewara and thanks for your time to write the php code. It is necessary to write to a file the data incoming from POST request to be able to use it later. Thanks again for your help also ESP_sprite.
Regards
Gastón