http get and post methods example in c

Get and Post method

The Hypertext Transfer Protocol (HTTP) is a stateless application layer protocol for distributed, collaborative, hypermedia information systems. In this article, I will explain, how you can create an HTTP get post request in C without using the library. But before going to create get post request let understand the what is Http protocol in a few words.

You can also check Articles,

HTTP use to exchange data over the reliable connection like TCP. In HTTP a client (program) try to establish a connection with other programs (Server) to send an HTTP request.

If the connection is established between the server and client then the server sends an Http response in order to the Http request. In HTTP mainly GET and POST method is used to send the request to the server.

What is the GET method?

HTTP get request is generally used to get data from the web-server. It has no side effect and it is not supposed to change anything on the server. So the GET method is idempotent. It can be executed more than once without any side effects.

Get method issued when you click a hyperlink or when you type an URL in an address bar and hit the enter key.

How to create HTTP GET Request in C?

The basic parameters of GET request are the path of resource and the Host header. The host header can be the domain name or IP address of the target machine, where we are sending the request.

Suppose there is a service:

http://www.aticleworld.com/2016/04/create-xml-request-in-c-for-server.html

When we prepared a GET request for the above URL then it would look like this.

GET /2016/04/create-xml-request-in-c-for-server.html HTTP/1.1
Host: www.aticleworld.com

Note: If the server runs only a single website on a single IP address then you can use IP address as header.

GET /2016/04/create-xml-request-in-c-for-server.html HTTP/1.1
Host: 74.125.28.121

Simple “C” program to create GET Request:

#include <stdio.h>


int main(int argc, char *argv[])
{

    char pszRequest[100]= {0};

    char pszResourcePath[]="2016/04/create-xml-request-in-c-for-server.html";

    char pszHostAddress[]="www.aticleworld.com";

    sprintf(pszRequest, "GET /%s HTTP/1.1\r\nHost: %s\r\nContent-Type: text/plain\r\n\r\n", pszResourcePath, pszHostAddress);

    printf("Created Get Request is below:\n\n\n");

    printf("%s", pszRequest);

    return 0;
}

 

OutPut:

post get request in C

 

 

 

 

 

 

 

If you want to learn more about the HTTP, here 10 Free days (up to 200 minutes) HTTP Fundamentals video course for you.

pluralsite

What is the POST method?

The POST method is used to submit data to the server. The post method is generally used where we want to post a message or submit information.

The POST method is secure followed by GET method because data does not transfer directly in the URL format as in GET method, data send in the message body of the request.

There is some extra header that describes the message body like its length and content type. Using the Get method we can send only ASCII character but using the POST method we can send ASCII and non-ASCII characters.

A browser cannot cache or bookmark the POST request data and there is no limitation of the length of request data.

How to create HTTP POST Request in C?

POST method contains some extra header that describes the content and the length of the request.

Suppose there is a service:

http://www.aticleworld.com/applicationform.svc/getdetail

When we prepared a POST request for the above URL then it would look like this.

POST http:// 74.125.28.121:80/applicationform.svc/getdetail HTTP/1.1

Host: 74.125.28.121:80

Content-Type: application/xml

Content-Length: 47

<body><name>amlendra</name><age>25</age></body>

 

Simple “C” program to create POST Request:

 

#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[])
{
    char aszXmlData[]="<body><name>amlendra</name><age>25</age></body>";
    char aszXmlRequest[250]= {0};
    char aszServiceMethod[]="applicationform.svc/getdetail";
    char aszRequest[150]= {0};
    char aszHostIp[30]="74.125.28.121";
    char aszPort[]="80";



    sprintf(aszRequest,"http://%s:%s/%s/%s HTTP/1.1",aszHostIp,aszPort,aszServiceMethod);

    printf("Method and Resource path is below:\n\n\n");

    printf("%s",aszRequest);

    strcat(aszHostIp,":");
    strcat(aszHostIp,aszPort);

    printf("\n\nHOST header is below:\n\n\n");
    printf("%s",aszHostIp);

    sprintf(aszXmlRequest,"POST %s\r\nHost: %s\r\nContent-Type: application/xml\r\nContent-Length: %d\r\n\r\n%s\r\n",aszRequest,aszHostIp,strlen(aszXmlData),aszXmlData);

    printf("\n\n\nPOST Request which send to the server:\n\n");
    printf("%s",aszXmlRequest);

    return 0;
}

 

OutPut:

get post request in C

 

 

Recommended Post for you



15 comments

  1. Hi Amlendra, I wanted to do trigger and action to IFTTT, Trigger is if temp >30 switch on AC (trigger parameter 30 OFF) and action parameter 30 ON.

    can you please help me …I am a java developer new to C.

  2. Hi, I am trying to trigger the camera by sending get request but its not working. Can anyone give me advise where I am doing wrong. The code is as follows:

    int main(int argc, char *argv[]) {

    char Request[100]={0};
    char ResourcePath[]=”/trigger/swtrigger?sendtrigger=1&wfilter=1″;
    char HostAddress[]=”192.168.2.171″;
    sprintf(Request, “GET /%s HTTP/1.1\r\n Host: %s\r\nContent-Type: text/plain\r\n\r\n”, ResourcePath,
    HostAddress);
    printf(“Created Get Request is below:\n\n\n”);
    printf(“%s”, Request);
    }

  3. Hi..I am trying to send some data on server using enc28j60 module with stm32. but the problem is that i don’t know how to do it. I just simply create a web page and monitor the led through that web page.Please guide me.

  4. I am not able to post my code here. Can you please help me to move out from this. Actually I am new on this and I don’t have any idea on it (I have understanding of all protocol like HTTP,TCP/IP). So can I contact you through email if you have some free time so that i completely explain my problem with you. i don’t have any friend circle on this field so i expected from you to help me. This is my first project so i don’t want to loose it.Problem is that i am not able to link the server with my stm32+enc28j60. I don’t take your much more time.so Plz help me.

Leave a Reply

Your email address will not be published. Required fields are marked *