
Client URL, or just curl, is a command-line tool for transferring data using various network protocols. It is commonly used by developers to test various applications build on top of HTTP.
That said, curl itself is just a wrapper around libcurl. The library is written in C and has well documented API. In this post, I will demonstrate how you can use libcurl in order to make HTTP requests from your C/C++ applications.
Before we begin, make sure you have C compiler installed. I will be using gcc. Other compilers will work too, but you will have to modify the provided Makefile. You will also need to have curl and libcurl installed. On Linux and OSX, if you can use curl command in your CLI you should be good to go.
Let's start by creating a simple Makefile:
Makefile
default:buildbuild:clean gcc-Wall-o curl main.c util.c-l curlclean:rm-rf curltest:build ./curl https://freegeoip.app/json/
Now, moving to the application. It is a simple CLI tool that takes an URL as an argument, makes HTTP Get request, and prints the response.
Here is the code:
main.c
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<curl/curl.h>#include"util.h"intmain(intargc,char*argv[]){if(argc!=2){printf("usage: try './curl [url]' to make a get request.\n");return1;}CURL*curl_handle;CURLcoderes;structMemoryStructchunk;chunk.memory=malloc(1);chunk.size=0;curl_handle=curl_easy_init();if(curl_handle){curl_easy_setopt(curl_handle,CURLOPT_URL,argv[1]);curl_easy_setopt(curl_handle,CURLOPT_FOLLOWLOCATION,1L);curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA,(void*)&chunk);curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"libcurl-agent/1.0");res=curl_easy_perform(curl_handle);if(res!=CURLE_OK){fprintf(stderr,"error: %s\n",curl_easy_strerror(res));}else{printf("Size: %lu\n",(unsignedlong)chunk.size);printf("Data: %s\n",chunk.memory);}curl_easy_cleanup(curl_handle);free(chunk.memory);}return0;}
util.h
#ifndef UTIL_H#define UTIL_H#include<stdio.h>#include<stdlib.h>#include<string.h>structMemoryStruct{char*memory;size_tsize;};size_tWriteMemoryCallback(void*contents,size_tsize,size_tnmemb,void*userp);#endif
util.c
#include"util.h"size_tWriteMemoryCallback(void*contents,size_tsize,size_tnmemb,void*userp){size_trealsize=size*nmemb;structMemoryStruct*mem=(structMemoryStruct*)userp;char*ptr=realloc(mem->memory,mem->size+realsize+1);if(ptr==NULL){printf("error: not enough memory\n");return0;}mem->memory=ptr;memcpy(&(mem->memory[mem->size]),contents,realsize);mem->size+=realsize;mem->memory[mem->size]=0;returnrealsize;}
The most interesting function here iscurl_easy_setopt
. It sets various options on the instance ofcurl
client (in my examplecurl_handle
). Note, that by settingCURLOPT_WRITEFUNCTION
andCURLOPT_WRITEDATA
we have configured thecurl_handle
to use custom logic and location for writing the response data.
Similarly, you can set custom headers, or HTTP Post payload. For the list of all the available options refer to thecurl docs.
Top comments(2)
For further actions, you may consider blocking this person and/orreporting abuse