how to access a body of POST request using fastcgi C/C++ application how to access a body of POST request using fastcgi C/C++ application nginx nginx

how to access a body of POST request using fastcgi C/C++ application


You can access the POST body via the FCGI_stdin stream. For example, you can read from it one byte at a time using FCGI_getchar, which is a short form for FCGI_fgetc(FCGI_stdin). You can read larger chunks of data in a single call using FCGI_fread. All of this I found looking at the source. These sources often reference something called ”H&S”—this stands for "Harbison and Steele", the authors of the book C: A Reference Manual, and the numbers refer to chapters and sections of that book.

And by the way, it's called “stdio” for “STanDard Input/Output”. Not “studio”. The functions should mostly behave like their counterparts without FCGI_ prefixed. So for details, look at the man pages of getchar, fread and so on.

Once you have the bytes in your application, you can write them to file, either using normal stdio operations or files opened via FCGI_fopen. Note however that the input stream will not directly correspond to the content of an uploaded file. Instead, MIME encoding is used to transfer all form data, including files. You'll have to parse that stream if you want to access the file data.


Use this:

char * method = getenv("REQUEST_METHOD");if (!strcmp(method, "POST")) {    int ilen = atoi(getenv("CONTENT_LENGTH"));    char *bufp = malloc(ilen);    fread(bufp, ilen, 1, stdin);    printf("The POST data is<P>%s\n", bufp);    free(bufp);}