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

北城以北 提交于 2019-11-28 00:01:29

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.

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!