STM32H745 freertos lwip simple web server POST Method issue
Hello All,
I have implemented a simple Web server using https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32756G_EVAL/Applications/LwIP/LwIP_HTTP_Server_Socket_RTOS example.
The Get method works fine but as soon as I use the POST method I get "The connection was reset" message in my web browser.
I am not too famuliar with http protocol so not sure of POST method needs to be treated differently. Below is just sime snip of the code
static void http_server_serve(struct netconn *conn)
{
static void http_server_serve(struct netconn *conn)
{
.
.
.
if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
{
/* Check if request to get ST.gif */
if (strncmp((char const *)buf,"GET /STM32F7xx_files/ST.gif",27)==0)
{
fs_open(&file, "/STM32F7xx_files/ST.gif");
netconn_write(conn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
fs_close(&file);
}
.
.
.
}
else if ((buflen >=6) && (strncmp(buf, "POST /", 6) == 0))
{
if (strncmp(buf, "POST /submit", 12) == 0)
{
// Find the start of the request body
char *body = strstr(buf, "\r\n\r\n");
if (body != NULL)
{
body += 4; // Skip the \r\n\r\n
// Extract Content-Length from headers
char *content_length_str = strstr(buf, "Content-Length:");
int content_length = 0;
if (content_length_str != NULL) {
sscanf(content_length_str, "Content-Length: %d", &content_length);
}
// Handle the POST request with dynamic memory allocation
handle_post_request(conn, body, content_length);
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
void handle_post_request(struct netconn *conn, char *request_body, int content_length) {
{
const char *response = "HTTP/1.0 201 OK\r\nContent-Type: text/html\r\n\r\n"
"<html><body>Form submitted successfully</body></html>";
netconn_write(conn, response, strlen(response), NETCONN_COPY);
}
Any guidance to guide me is deeply appreciated.
