How to retrieve form "POST" data via cgi-bin program written in C

Athabaska Dick picture Athabaska Dick · Mar 27, 2011 · Viewed 17k times · Source

I am trying to retrieve POST data from html form using program written in C.

At the moment I am using:

char *formdata = getenv("QUERY_STRING");
if(formdata == NULL) /* no data retrieved */

This seems to be working fine with form "GET" method but not with "POST" method. How do I retrieve POST data?

Answer

pmg picture pmg · Mar 27, 2011

If I remember right, read stdin for POST data.


Edit for untested snippet

len_ = getenv("CONTENT_LENGTH");
len = strtol(len_, NULL, 10);
postdata = malloc(len + 1);
if (!postdata) { /* handle error or */ exit(EXIT_FAILURE); }
fgets(postdata, len + 1, stdin);
/* work with postdata */
free(postdata);