A simple HTTP request library written in pure C
git clone https://github.com/jozanza/librequest.git
cd librequest
make install
librequest
depends on libopenssl
, so you will have to add the following flags to your compiler:
-I/usr/local/opt/openssl/include
-L/usr/local/opt/openssl/lib
-lcrypto
-lssl
-lrequest
librequest
supports both synchronous (blocking) and async (non-blocking) HTTP requests 🎉
#include <request.h>
#include <stdio.h>
int main() {
HTTPRequest req = {
.method = HTTPRequestMethod_GET,
.port = HTTPRequestPort_DEFAULT_HTTPS,
.hostname = "postman-echo.com",
.pathname = "/get?foo1=bar1&foo2=bar2",
};
HTTPResponse* res = Request(req);
printf("[Status]: %d\n\n", res->status);
printf("[Headers]:\n%s\n\n", res->headers);
printf("[Body]:\n%s\n\n", res->body);
// NOTE: Caller is responsible for freeing the
// response when making a synchronous request
FreeResponse(&res);
return 0;
}
#include <request.h>
#include <stdio.h>
#include <unistd.h>
void onComplete(HTTPResponse* res) {
printf("[Status]: %d\n\n", res->status);
printf("[Headers]:\n%s\n\n", res->headers);
printf("[Body]:\n%s\n\n", res->body);
// NOTE: The response is automatically freed
// once the onComplete callback returns
}
int main() {
HTTPRequest req = {
.method = HTTPRequestMethod_GET,
.port = HTTPRequestPort_DEFAULT_HTTPS,
.hostname = "postman-echo.com",
.pathname = "/get?foo1=bar1&foo2=bar2",
};
RequestAsync(req, onComplete);
sleep(1);
}