esp32 http POST question

Gaston1980
Posts: 28
Joined: Sun Oct 06, 2024 10:26 am

esp32 http POST question

Postby Gaston1980 » Sun Nov 10, 2024 2:37 am

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

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32 http POST question

Postby ESP_Sprite » Sun Nov 10, 2024 4:37 am

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.

Gaston1980
Posts: 28
Joined: Sun Oct 06, 2024 10:26 am

Re: esp32 http POST question

Postby Gaston1980 » Sun Nov 10, 2024 1:54 pm

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
Attachments
post.png
post.png (10.28 KiB) Viewed 1098 times

Gaston1980
Posts: 28
Joined: Sun Oct 06, 2024 10:26 am

Re: esp32 http POST question

Postby Gaston1980 » Sun Nov 10, 2024 11:58 pm

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

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: esp32 http POST question

Postby boarchuz » Mon Nov 11, 2024 2:43 am

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?

Gaston1980
Posts: 28
Joined: Sun Oct 06, 2024 10:26 am

Re: esp32 http POST question

Postby Gaston1980 » Mon Nov 11, 2024 10:10 am

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

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: esp32 http POST question

Postby chegewara » Wed Nov 13, 2024 3:22 am

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.

Gaston1980
Posts: 28
Joined: Sun Oct 06, 2024 10:26 am

Re: esp32 http POST question

Postby Gaston1980 » Wed Nov 13, 2024 1:24 pm

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

Who is online

Users browsing this forum: Bryght-Richard and 213 guests