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,
- Parse the XML response in C without using the library.
- Create an XML request in C without using the library.
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:
If you want to learn more about the HTTP, here 10 Free days (up to 200 minutes) HTTP Fundamentals video course for you.
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:
Recommended Post for you
- socket programming in C.
- HDLC Protocol in C.
- SSL programming in C.
- Parse XML response in C without using the library.
- Create Http Get and Post request in C.
- A brief description of Array in C
- A brief description of the pointer in C.
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- void pointer in C.
- 10 questions about dynamic memory allocation.
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.
- C format specifiers.