How to retrieve form “POST” data via cgi-bin progr

2020-01-29 08:55发布

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?

3条回答
劳资没心,怎么记你
2楼-- · 2020-01-29 09:13

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

查看更多
相关推荐>>
3楼-- · 2020-01-29 09:34

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.

查看更多
forever°为你锁心
4楼-- · 2020-01-29 09:34

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);
查看更多
登录 后发表回答