cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H745 freertos lwip simple web server POST Method issue

HKapo.2
Associate II

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
LCE
Principal

Just wanted to add:

check lwip's httpd, POST is "prepared".

Google for lwip POST example, there's something almost usable for http_post_finished and other functions.

View solution in original post

6 REPLIES 6

@HKapo.2 wrote:

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.


Does that claim to support POST ?

 


@HKapo.2 wrote:

The Get method works fine but as soon as I use the POST method ...


What are you using to issue those requests?

 


@HKapo.2 wrote:

I am not too familiar with http protocol


Start here: https://www.jmarshall.com/easy/http/

About POST, specifically: https://www.jmarshall.com/easy/http/#postmethod 

Also, tools like curl and wget are good for testing & gaining familiarity ...

Thanks Andrew for quick response.

 

I have used packet sender to ensure that a response to Post is being received.

HKapo2_0-1722003673664.png

But when I try submiting the form via web browser I get "The connection was reset" error.

 

Thanks for the link, I will go through it

 

 

 

 

The example does say simple Web server.

So maybe its response is just sufficient to be valid as far as the protocol is concerned, but not enough to give the full interaction that your browser is expecting?

Thanks Andrew,

 

I am now using httpd example as a base for my project.

LCE
Principal

Just wanted to add:

check lwip's httpd, POST is "prepared".

Google for lwip POST example, there's something almost usable for http_post_finished and other functions.

HKapo.2
Associate II

Thanks for the suggestions.

 

I got everyting up and running now.