libfetch 0.0.0
A lightweight asynchronous HTTP/1.1 client library implementing a subset of the WHATWG Fetch API.
Loading...
Searching...
No Matches
Core Fetch API

Main functions for making HTTP requests. More...

Functions

fetch_response_tfetch (const char *url, fetch_init_t *init)
 Make a synchronous HTTP request (BLOCKING)
 
fetch_promise_tfetch_async (const char *url, fetch_init_t *init)
 Make an asynchronous HTTP request (NON-BLOCKING)
 

Detailed Description

Main functions for making HTTP requests.

Function Documentation

◆ fetch()

fetch_response_t * fetch ( const char * url,
fetch_init_t * init )

Make a synchronous HTTP request (BLOCKING)

This function blocks the calling thread until the request completes or fails. The event loop runs internally during this call.

Parameters
urlThe URL to fetch
initRequest configuration options (can be NULL for defaults, borrowed reference)
Returns
Response object (caller owns, must call fetch_response_free()), or NULL on memory allocation failure
Warning
This function BLOCKS the calling thread until completion
Note
Memory: Returns owned response object that must be freed
Threading: Must be called from the same thread as the event loop
// Simple blocking GET request
fetch_response_t *response = fetch("https://httpbin.org/get", NULL);
if (response) {
if (fetch_response_ok(response)) {
printf("Status: %d\n", fetch_response_status(response));
const char *text = fetch_response_text(response);
if (text) {
printf("Body: %s\n", text);
}
} else {
printf("Request failed: %d %s\n", fetch_response_status(response),
}
fetch_response_free(response); // Required: free the response
}
fetch_response_t * fetch(const char *url, fetch_init_t *init)
Make a synchronous HTTP request (BLOCKING)
Definition fetch.c:7835
void fetch_response_free(fetch_response_t *response)
Free a response object.
Definition fetch.c:8389
bool fetch_response_ok(const fetch_response_t *response)
Check if response is successful (2xx status)
Definition fetch.c:8369
const char * fetch_response_text(fetch_response_t *response)
Get response body as text.
Definition fetch.c:8329
const char * fetch_response_status_text(const fetch_response_t *response)
Get HTTP status text.
Definition fetch.c:8377
uint16_t fetch_response_status(const fetch_response_t *response)
Get HTTP status code.
Definition fetch.c:8373
HTTP response object.
Definition fetch.h:515

◆ fetch_async()

fetch_promise_t * fetch_async ( const char * url,
fetch_init_t * init )

Make an asynchronous HTTP request (NON-BLOCKING)

Returns immediately with a promise. The request proceeds in the background and must be driven by calling fetch_event_loop_process() regularly.

Parameters
urlThe URL to fetch
initRequest configuration options (can be NULL for defaults, borrowed reference)
Returns
Promise object (caller owns, must call fetch_promise_free()), or NULL on memory allocation failure
Note
This function returns immediately and does NOT block
Memory: Returns owned promise object that must be freed
Event Loop: Requires fetch_event_loop_process() to be called regularly for progress
// CORRECT: Non-blocking usage with event loop driving
fprintf(stderr, "Failed to start event loop\n");
return -1;
}
fetch_promise_t *promise = fetch_async("https://httpbin.org/get", NULL);
if (!promise) {
return -1;
}
// Drive the event loop until completion (non-blocking approach)
while (fetch_promise_pending(promise)) {
int events = fetch_event_loop_process(100); // 100ms timeout
if (events < 0) {
printf("Event loop error\n");
break;
}
// Do other work here while request is in progress
printf("Request in progress...\n");
}
// Check result
if (fetch_promise_fulfilled(promise)) {
if (response && fetch_response_ok(response)) {
printf("Success: %s\n", fetch_response_text(response));
}
// Note: response is owned by promise, don't free it separately
} else if (fetch_promise_rejected(promise)) {
printf("Failed: %s\n", fetch_promise_error_message(promise));
}
fetch_promise_free(promise); // Required: free the promise
fetch_promise_t * fetch_async(const char *url, fetch_init_t *init)
Make an asynchronous HTTP request (NON-BLOCKING)
Definition fetch.c:7764
int fetch_event_loop_process(uint32_t timeout_ms)
Process events in the event loop (NON-BLOCKING with timeout)
Definition fetch.c:7339
void fetch_event_loop_stop(void)
Stop the event loop.
Definition fetch.c:7436
bool fetch_event_loop_start(void)
Start the event loop.
Definition fetch.c:7407
void fetch_promise_free(fetch_promise_t *promise)
Free a promise object.
Definition fetch.c:8401
const char * fetch_promise_error_message(const fetch_promise_t *promise)
Get the error message from a rejected promise.
Definition fetch.c:8012
bool fetch_promise_rejected(const fetch_promise_t *promise)
Check if promise was rejected.
Definition fetch.c:8024
fetch_response_t * fetch_promise_response(const fetch_promise_t *promise)
Get the response from a fulfilled promise.
Definition fetch.c:8000
bool fetch_promise_fulfilled(const fetch_promise_t *promise)
Check if promise was fulfilled successfully.
Definition fetch.c:8020
bool fetch_promise_pending(const fetch_promise_t *promise)
Check if promise is still pending.
Definition fetch.c:8016
Promise for asynchronous fetch operations.
Definition fetch.h:563