Commit 60cddcc9 authored by Tiago Medicci's avatar Tiago Medicci

Added POST example function.

parent cf6c707e
...@@ -29,6 +29,11 @@ Examples functions at http server ...@@ -29,6 +29,11 @@ Examples functions at http server
* Server initialization added into the example function, simply call it and it should work! * Server initialization added into the example function, simply call it and it should work!
* Receiving a GET request at /, http server response is a "Hello World, from ESP32!" html. * Receiving a GET request at /, http server response is a "Hello World, from ESP32!" html.
## POST Method Example
`simple_POST_method_example()` function:
* As well as GET example, simply add http_server as a componente into your ESP-IDF project.
* Server initialization added into the POST example function, simply call it and don't worry.
* Post to / a pair of key-value where the key is 'key' and value some value you want to test. The example will show value content. If needed, increade log verbosity at `make menuconfig` to show all parsed key-value pairs.
# Debugging # Debugging
......
...@@ -126,6 +126,9 @@ const static char index_html[] = "<!DOCTYPE html>" ...@@ -126,6 +126,9 @@ const static char index_html[] = "<!DOCTYPE html>"
"</body>\n" "</body>\n"
"</html>\n"; "</html>\n";
const static char response_OK[] =
"OK!\n";
esp_err_t http_register_handler(http_server_t server, esp_err_t http_register_handler(http_server_t server,
const char* uri_pattern, int method, const char* uri_pattern, int method,
...@@ -862,3 +865,43 @@ esp_err_t simple_GET_method_example(void) ...@@ -862,3 +865,43 @@ esp_err_t simple_GET_method_example(void)
return res; return res;
} }
static void cb_POST_method(http_context_t http_ctx, void* ctx)
{
const char* post_data;
ESP_LOGI(TAG, "Received data from POST method...");
/*Receiving key from POST*/
post_data = http_request_get_arg_value(http_ctx, "key");
if(post_data!=NULL){
ESP_LOGI(TAG, "Received %d bytes corresponding to the 'key': %s", strlen(post_data), post_data);
}else{
ESP_LOGI(TAG, "Received NULL from POST method");
}
size_t response_size = strlen(response_OK);
http_response_begin(http_ctx, 201, "text/plain", response_size);
http_buffer_t http_response_OK = { .data = response_OK };
http_response_write(http_ctx, &http_response_OK);
http_response_end(http_ctx);
}
esp_err_t simple_POST_method_example(void)
{
http_server_t server;
http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT();
esp_err_t res;
res = http_server_start(&http_options, &server);
if (res != ESP_OK) {
return res;
}
res = http_register_form_handler(server, "/", HTTP_POST, HTTP_HANDLE_RESPONSE, &cb_POST_method, NULL);
if (res != ESP_OK) {
return res;
}
return res;
}
...@@ -294,6 +294,14 @@ esp_err_t http_response_end(http_context_t http_ctx); ...@@ -294,6 +294,14 @@ esp_err_t http_response_end(http_context_t http_ctx);
*/ */
esp_err_t simple_GET_method_example(void); esp_err_t simple_GET_method_example(void);
/**
* @brief Example of POST method. Send a application/x-www-form-urlencoded pair key-value where the key is 'key' and some value for it. The value is printed and the server responds a 201 code and a OK message.
* @param none
* @return
* - ESP_OK on success
* - other errors in the future?
*/
esp_err_t simple_POST_method_example(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment