Skip to main content
Associate III
January 21, 2025
Solved

http client lwip stm32h745-DISCO

  • January 21, 2025
  • 1 reply
  • 1475 views

Hello!

I need to implement a task in FreeRTOS for my project that makes GET, POST, and PATCH requests to a server. So far, I have managed to send a UDP message to a specific port on a PC thanks to the following post:

https://community.st.com/t5/stm32-mcus/how-to-create-a-project-for-stm32h7-with-ethernet-and-lwip-stack/ta-p/49308

 

Now, I would like to implement the task to send the requests. I have searched the forum but haven't found a clear conclusion. What catches my attention is that in the project I created with STM32CubeMX, there is the file http_client.h in Middleware -> Third_Party -> LwIP -> src -> include -> apps, but I can't find the http_client.c file with the functions.

I have found in the following link (https://github.com/yarrick/lwip/blob/master/src/apps/http/http_client.c) I have copy the http folder in  Middleware -> Third_Party -> LwIP -> src -> apps.

And I have tried with the following code:

 

void http_get_task(void const *pvParameters) {
 // Initialize the LWIP stack (only if not already initialized elsewhere)
 lwip_init();

 // HTTP connection settings
 httpc_connection_t settings;
 settings.use_proxy = 0; // No proxy
 settings.result_fn = http_result_callback;
 settings.headers_done_fn = http_headers_done_callback;

 ip_addr_t server_addr;
 IP4_ADDR(&server_addr, 192, 168, 4, 250); // Server IP address
 const char *uri = "/path/to/resource"; // Resource URI
 u16_t port = 3000; // Server port

 httpc_state_t *connection = NULL;

 // Make the HTTP GET request
 err_t err = httpc_get_file(&server_addr, port, uri, &settings, NULL, NULL, &connection);
 if (err != ERR_OK) {
 printf("Failed to start HTTP GET request. Error: %d\n", err);
 vTaskDelete(NULL); // Delete the task if the request fails
 return;
 }

 printf("HTTP GET request initiated. Waiting for response...\n");

 // Main task loop to handle timeouts and connection
 while (connection != NULL) {
 sys_check_timeouts(); // Process LWIP timeouts
 vTaskDelay(pdMS_TO_TICKS(100)); // Delay to avoid busy-waiting
 }

 printf("Task complete. Exiting.\n");
 vTaskDelete(NULL); // Delete the task when done
}

 

When compilig the next error appears: undefined reference to `httpc_get_file' main.c. I have included the http_client.h in main.c (#include "lwip/apps/http_client.h"). Also I have verified that the macros  LWIP_TCP && LWIP_CALLBACK_API are at 1.

Apart from these compilation issues, I wonder if I am heading in the right direction or if there is another way to set up an HTTP client. Any help is welcome, thanks. :)

Best answer by RafaelS

Hello!

Nom I am able to make a PATCH request to my server. I have tested since the weekend. 2.000.000 PATCH request done without any error.

I attach the modified http_client.c file. I have created the httpc_patch_file function in orther to allow to send a token in the header and a json body. 

I hope this helps somebody

1 reply

Andrew Neil
Super User
January 21, 2025

@RafaelS wrote:

When compiling the next error appears: undefined reference to `httpc_get_file' main.c.


It's telling you that it can't find any definition for the httpc_get_file() function.

 


@RafaelS wrote:

I have included the http_client.h in main.c (#include "lwip/apps/http_client.h")


A header file would not be expected to provide a definition - just a declaration (aka "prototype").

So do you actually have a .c file in your project which does define the httpc_get_file() function?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
RafaelSAuthor
Associate III
January 21, 2025

Hello Andrew,

I have download the http folder from https://github.com/yarrick/lwip/tree/master/src/apps/http,

and  after I have added the path of the folder in the Include paths of MCU/MPU GCC Compiler.

 

In the http folder there is an http_client.c that define the httpc_get_file https://github.com/yarrick/lwip/blob/master/src/apps/http/http_client.c in line 641.

RafaelSAuthorBest answer
Associate III
January 28, 2025

Hello!

Nom I am able to make a PATCH request to my server. I have tested since the weekend. 2.000.000 PATCH request done without any error.

I attach the modified http_client.c file. I have created the httpc_patch_file function in orther to allow to send a token in the header and a json body. 

I hope this helps somebody