Run C++ as CGI script in web browser

Last updated : Oct 05, 2021

In this article you will get to know how to create and run C++ as CGI script and execute it in Apache web server.


It's very unusual these days because CGI scripts are not frequently used. PHP, ASP, Java, Python are languages which are frequently used to serve with dynamic responses to requests.


What is CGI ?

1. Common Gateway Interface(CGI) is an interface which communicates with other programs running on the server.

2. CGI script can take input through stdin such as form data and query string.

3. The program then processes the data and sends the output back to server which then converts output into proper http response.


Let's create a CGI script in C++ and execute in Apache web server.


1. Firstly download and install Xampp package which will install all the necessary services for building other web projects also.


2. Since we are creating a script in C++ so we need a compiler to create an executable but we don't need .exe file rather we need a .cgi file. Further we will see how to get .cgi file.


3. Then we will create a script. Open any code, we are using VS code here. And type in the following code.

#include<iostream>
#include<cstring>

using namespace std;

int main(){
    cout << "Content-type:application/json\n\n";

    if(strcmp(getenv("REQUEST_METHOD"),"POST")==0){
        int len;
        char* lenstr = getenv("CONTENT_LENGTH");

        if(lenstr != NULL && (len = atoi(lenstr)) != 0){
            char* post_data = new char[len];
            fgets(post_data,len+1,stdin);
            cout << post_data;
        }
    }
    else if(strcmp(getenv("REQUEST_METHOD"),"GET")==0){
        cout << getenv("QUERY_STRING") << endl;
    }
    else{
        cout << "request is not supported";
    }
    return 0;
}



Now let's break the code and unserstand the working.


1. First 2 lines are used to include header files.


2. In line no.4, we are using namespace std because we are pointing towards new compiler.


3. In line no.6 is a main() function to give a point for compilation initializing.


4. In line no.7 is given a page header information in which content-type is included to return response only in JSON mode.


5. From line no.9 to 24 is the actual script to handle the request type, if it is post then we get the content and convert it into integer with atoi() function to check the length and if there is data we will fetch it from standard stream i.e., stdin and place it in a pointer variable and then displayed the response.


6. And if it is get request then we simply get the data from getenv() function which is an environment function and by using QUERY_STRING inside it.


Finishing up the program


1. Download and install MinGW compiler and check it by typing gcc --version or g++ --version and if everything's fine and running, it will display version else you have to setup compiler.


2. Next, copy your code file and paste it in the whatever drive/xampp/cgi-bin directory.


3. Open cmd and go to the same directory mentioned above and type in g++ codefilename.cpp -o sample.cgi.


It's time for testing now. Open postman or any other api testing program


1. GET request




2. POST request




Sign in for comment. Sign in