HTTP Client: Go Fetch - MisterRager/Codelab-Indicator-Lights GitHub Wiki

Making HTTP Requests

The official http client makes non-blocking http requests that report esp_http_client_event_t events back to a callback function. The function data_get in src/data/data_http.h collects data from into a fixed 32KiB buffer from the HTTP_EVENT_ON_DATA events until an HTTP_EVENT_ON_FINISH event arrives. Then, the buffer is parsed with cJSON. After parsing, if the http_request had an on_success_fn set, the cJSON * parsed from the result is passed back.

The only implementation of a request provided is a fetch from the National Weather Service which uses values hardcoded from the Pinterest office coordinates. It was chosen because it is a free API with no authentication.

Also of note, these are fairly heavyweight operations: they should generally get their own task:

static void on_receive_forecasts(cJSON *root)
{
}

static void task_fetch_forecasts(void *unused)
{
    fetch_forecasts(3, "TOP", 31, 80, on_receive_forecasts);
}

// Wait for network to be connected before attempting HTTP requests
static void on_network_available()
{
    xTaskCreate(task_fetch_forecasts, "FetchForecasts", 4096, NULL, 5, NULL);
}

void app_main()
{
    register_network_ready_listener(on_network_available);
    connect_wifi();
}

Here, the task that makes the HTTP request is triggered by the "network is ready" callback because the request will fail otherwise.