Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using Libcurl in C/C++ Application
Artem
Artem

Posted on • Edited on

     

Using Libcurl in C/C++ Application

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/
Enter fullscreen modeExit fullscreen mode

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;}
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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;}
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
trunc8 profile image
trunc8
Hi, this is trunc8.
  • Joined

Thanks for the article! One correction in the Makefile:gcc -Wall -o curl main.c util.c -lcurl
The library linking must be done at the end.Reference

CollapseExpand
 
secure_daily profile image
Artem
  • Location
    Santa Rosa, CA
  • Education
    San Francisco State University
  • Work
    Cloud Security Engineer at Virtru
  • Joined

Awesome! Thank you for this correction

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Santa Rosa, CA
  • Education
    San Francisco State University
  • Work
    Cloud Security Engineer at Virtru
  • Joined

More fromArtem

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp