Run C++ as CGI script in web browser

Last updated : Oct 04, 2021

Run C++ as CGI script in web browser



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




How video streaming works on t...

Read more

Setup react and express on sam...

Read more

How neurons in brain function

Read more

How to create custom router in...

Read more

Real time chat application usi...

Read more

How to setup lemp stack in ubu...

Read more

How to secure phpmyadmin in Ub...

Read more

How to create real time notifi...

Read more

How to create simple api in no...

Read more

Task scheduling with cron job ...

Read more

How to use modules in Javascri...

Read more

Create a multi language(lingua...

Read more

Create food ordering app - Foo...

Read more

Linear Regression in machine l...

Read more

Create simple web application ...

Read more

Why algorithms can drive the i...

Read more

Why Javascript frameworks are ...

Read more

Create authenticated API in La...

Read more

Create simple and static websi...

Read more

Setup Pusher app

Read more

Laravel Installation and setti...

Read more

Realtime chat application in L...

Read more

Working of neurons (the easy w...

Read more

Progressive web app

Read more

Sign in for comment. Sign in