Get FastCGI post body data using C

0
5015
FastCGI
FastCGI

FastCGI is basically not different from a regular CGI application, hence, you can get the post body content from stdin. However, we need to determine the length of the post content before getting the content, and that can be done by querying CONTENT_LENGTH variable for the size of data.


char *get_post_body()
{
    char *content_length = getenv("CONTENT_LENGTH");
    if(!content_length) {
        return "";
    }
    int size = atoi(content_length);
    char *body = (char *) calloc(size + 1, sizeof(char));
    fread(body, size, 1, stdin);
    return body;
}

That’s it!