Main functions for making HTTP requests.
More...
Main functions for making HTTP requests.
◆ fetch()
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
-
url | The URL to fetch |
init | Request 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
if (response) {
if (text) {
printf("Body: %s\n", text);
}
} else {
}
}
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()
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
-
url | The URL to fetch |
init | Request 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
fprintf(stderr, "Failed to start event loop\n");
return -1;
}
if (!promise) {
return -1;
}
if (events < 0) {
printf("Event loop error\n");
break;
}
printf("Request in progress...\n");
}
}
}
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