12

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?

Athabaska Dick
  • 3,855
  • 3
  • 20
  • 22

3 Answers3

16

POST data is appended to the request header, after a double newline. In a CGI-BIN environment, you read it from STDIN.

Be warned that the server IS NOT REQUIRED to send you an EOF character (or some termination indicator) at the end of the POST data. Never read more than CONTENT_LENGTH bytes.

salezica
  • 74,081
  • 25
  • 105
  • 166
14

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);
pmg
  • 106,608
  • 13
  • 126
  • 198
4

Why reinvent that wheel? Just use a library: http://libcgi.sourceforge.net/

Sherm Pendley
  • 13,556
  • 3
  • 45
  • 57
  • 6
    +1 ... but reinventing the wheel is a great way to learn stuff ... and maybe ... someday ... come up with a different better way to do the same old things :) – pmg Mar 27 '11 at 20:07
  • Hell yeah, pmg! Just yesterday I told someone: if you want to learn and have the time, reinvent the wheel when you get the chance! – salezica Mar 27 '11 at 20:11
  • Finding a better way to do this would require being **very** familiar with the existing ways to do it - someone who needs to ask how to retrieve POST data doesn't have that level of familiarity. – Sherm Pendley Mar 27 '11 at 20:11
  • In this particular case I just used _getenv()_ to retrieve the content length and then read the whole dataset with _fread()_. – Athabaska Dick Mar 27 '11 at 21:08
  • I checked how libcgi retrieves the POST data and it was exactly the same way I did it. – Athabaska Dick Mar 27 '11 at 22:01
  • What about decoding the data? Splitting it up into name/value pairs? Handling different text encodings, such as UTF-8 vs. ISO-8859-1? Or file uploads? No disrespect intended, but the fact that you think a simple fread() of the data (the easiest part) is meaningful simply proves my point. – Sherm Pendley Mar 27 '11 at 22:37
  • 2
    Yeah I get your point but this particular project does not need any support for character decoding or any other wizardy. Even the standalone version of the app is requiring that the input data is pure ASCII (accepting only characters between a to z). I am also handling the html escape codes in a more efficient and suitable way for my dataset than libcgi. Libcgi has support only for escape sequence encoding & decoding and base64 and I dont need either of them now. Ofcourse libcgi or similar libs comes in handy when dealing with more feature heavy app. – Athabaska Dick Mar 28 '11 at 04:25
  • OK, you do have a point - pulling in a whole library adds to your code's footprint, and may be overkill if your requirements are simple. – Sherm Pendley Mar 28 '11 at 04:30
  • 3
    Link only answers should be posted as comments. That and this does not answer the question. – bobobobo Apr 11 '13 at 23:08