如何通过用C编写的cgi-bin程序检索表单“POST”数据

时间:2021-01-20 19:23:46

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

我正在尝试使用用C编写的程序从html表单中检索POST数据。

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?

这似乎与表单“GET”方法一起使用,但不适用于“POST”方法。如何检索POST数据?

3 个解决方案

#1


9  

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

在双换行符之后,POST数据将附加到请求标头。在CGI-BIN环境中,您可以从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.

请注意,服务器不需要在POST数据的末尾向您发送EOF字符(或某些终止指示符)。从不读取超过CONTENT_LENGTH个字节。

#2


10  

If I remember right, read stdin for POST data.

如果我没记错,请阅读stdin获取POST数据。


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

#3


4  

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

为什么重新发明那个*?只需使用一个库:http://libcgi.sourceforge.net/

#1


9  

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

在双换行符之后,POST数据将附加到请求标头。在CGI-BIN环境中,您可以从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.

请注意,服务器不需要在POST数据的末尾向您发送EOF字符(或某些终止指示符)。从不读取超过CONTENT_LENGTH个字节。

#2


10  

If I remember right, read stdin for POST data.

如果我没记错,请阅读stdin获取POST数据。


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

#3


4  

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

为什么重新发明那个*?只需使用一个库:http://libcgi.sourceforge.net/