Bitty HTTP

Development » POST Vars

1Reading POST vars

This example will show reading POST vars. These are basically the same as GET vars but using WS_POST() instead of WS_GET()

Bitty HTTP stores POST vars in a block of RAM as the headers are processed (just after the GET vars). This means it needs to know the names of all the POST vars before the page is requested. This is done by filling in the struct WSPageProp.Posts variable when the page properties are requested.

The Posts property (just like the Gets) is an array of pointers to strings with the last one being set to NULL to mark the end of the list. This list is scanned when the page is requested and the value of any vars in this list are added to a RAM array.

You then call WS_POST() to get the value that was saved (or NULL if it wasn't found).

Using things this way means a low memory foot print as we only remember vars that we are going to pull out later.

Files

FileServer.c

The file server is changed to provide a form on the root page and a new page to display the values from the form.

const char *PostArgs[]= { "Name", NULL };

We add a list of POST vars that we may want to read when processing a page later. We end the list with NULL to mark the end of the list. The code will loop though this list looking for a NULL entry to stop at.

When a match is made the value of the var will be copied to an internal RAM buffer.

struct FileInfo m_Files[]= { /* Filename, Dynamic, Cookies, Gets, Posts, Callback */ {"/",false,NULL,NULL,NULL,File_Root}, {"/Display.html",true,NULL,NULL,PostArgs,File_Display}, {"/SomeStyle.css",false,NULL,NULL,NULL,File_SomeStyle}, {"/quit.html",true,NULL,NULL,NULL,File_Quit}, };

A new page has been added called /Display.html that will display what the user has typed into the form from the root page. This includes a pointer to the PostArgs as the 5'th argument. If you wanted to use both GET and POST vars you would provide to different arrays (one for POSTand one for GET vars).

const char RootHTML[]= "<!DOCTYPE html>" "<html>" "<head>" "<title>Bitty HTTP - Root</title>" "<link rel='stylesheet' href='/SomeStyle.css'>" "</head>" "<body>" "<div id='top'>Bitty HTTP Example - Root</div>" "<div id='quitbttn'><a href='/quit.html'>QUIT</a></div>" "<div id='content'>" "POST example<br/>" "<br/>" "<form action='/Display.html' method='POST'>" "Input your name:<input name='Name'></input> " "<input type='submit'></input>" "</form>" "<br/>" "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Root(struct WebServer *Web) { WS_WriteWhole(Web,RootHTML,sizeof(RootHTML)-1); }

This now just outputs a basic form with a get method asking the user for their name and going to /Display.html.

const char DisplayHTML_Start[]= "<!DOCTYPE html>" "<html>" "<head>" "<title>Bitty HTTP - Root</title>" "<link rel='stylesheet' href='/SomeStyle.css'>" "</head>" "<body>" "<div id='top'>Bitty HTTP Example - Root</div>" "<div id='quitbttn'><a href='/quit.html'>QUIT</a></div>" "<div id='content'>"; const char DisplayHTML_End[]= "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Display(struct WebServer *Web) { char buff[100]; const char *Name; WS_WriteChunk(Web,DisplayHTML_Start,sizeof(DisplayHTML_Start)-1); Name=WS_POST(Web,"Name"); if(Name==NULL) { WS_WriteChunk(Web,"Name was not set.",17); } else { sprintf(buff,"Your name is:%s",Name); WS_WriteChunk(Web,buff,strlen(buff)); } WS_WriteChunk(Web,"<br/><br/><a href='/'>Try again</a>",35); WS_WriteChunk(Web,"<br/>",5); WS_WriteChunk(Web,DisplayHTML_End,sizeof(DisplayHTML_End)-1); }

File_Display() uses WS_POST() to read out the get vars from internal RAM. You call it with the name of the POST var to read. This name also has to be in the PostArgs array as well.

If WS_POST() does not find the var (because it wasn't passed in) it returns NULL.

It WS_POST() does find the var then it returns a string pointer to the value that was read.