HTTP Client#
This module contains the HTTP Client API functions.
Note
Event/Callback handlers must not contain function calls or code which can block or delay the execution of the event/callback handler as it will cause all the other events to queue up and delay the execution of other events since all the events are invoked and handled from a single thread.
Do not call any synchronous SDK APIs from within the Event/Callback handlers.
Functions#
Initializes an HTTP client.
Deinitializes an HTTP client and releases resources used by the HTTP client.
Initializes a callback function for the specified HTTP request.
Adds an extended header with a key and value to the client request.
Deletes a specified header field from the extended headers of an HTTP client request.
Deletes all headers from the extended headers of an HTTP client request.
Sends an HTTP request.
Sends HTTP POST and PUT chunked data.
Function Documentation#
sl_http_client_init#
sl_status_t sl_http_client_init (const sl_http_client_configuration_t * configuration, sl_http_client_t * client)
Initializes an HTTP client.
[in] | configuration | Pointer to an sl_http_client_configuration_t structure containing the configuration settings for the HTTP client. Must not be NULL. |
[out] | client | Pointer to an sl_http_client_t object that would be initialized with the client handle. Must not be NULL. |
It prepares the client to send HTTP requests. You should call this function before making any HTTP requests to ensure the client is properly configured.
Note
You can call this function multiple times to initialize multiple HTTP client resources.
You can use this function after calling
deinit()
to reinitialize the resource.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to initialize the HTTP client.
328
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_deinit#
sl_status_t sl_http_client_deinit (const sl_http_client_t * client)
Deinitializes an HTTP client and releases resources used by the HTTP client.
[in] | client | Pointer to an sl_http_client_t object representing the HTTP client handle. Must not be NULL. |
This function deinitializes the HTTP client, freeing any resources that were allocated during its initialization and usage. It also deletes any extended headers that may exist.
The HTTP client must be initialized using sl_http_client_init before calling this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: The provided client handle is NULL or invalid.
SL_STATUS_FAIL: Failed to deinitialize the HTTP client.
Note
The user must call this function to release resources once the HTTP client is no longer needed.
354
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_request_init#
sl_status_t sl_http_client_request_init (sl_http_client_request_t * request, sl_http_client_event_handler_t event_handler, void * request_context)
Initializes a callback function for the specified HTTP request.
[in] | request | Pointer to an sl_http_client_request_t structure containing the HTTP client request configuration. Must not be NULL. |
[in] | event_handler | Callback function of type sl_http_client_event_handler_t to handle HTTP client events. Must not be NULL. |
[in] | request_context | User-defined context pointer. The memory space for this context must remain valid until the response is received. |
This function sets up a callback function to handle events for a specific HTTP request. It must be called after initializing the HTTP client.
sl_http_client_init should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to initialize the HTTP client request.
382
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_add_header#
sl_status_t sl_http_client_add_header (sl_http_client_request_t * request, const char * key, const char * value)
Adds an extended header with a key and value to the client request.
[in] | request | Pointer to an sl_http_client_request_t structure representing the HTTP client request configuration. Must not be NULL. |
[in] | key | Pointer to a string containing the header key. Must not be NULL. |
[in] | value | Pointer to a string containing the header value. Must not be NULL. |
This function adds an extended header to the specified HTTP client request. If the request does not already contain any extended headers, this function allocates memory for them.
sl_http_client_init should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to add the header to the request.
Note
If the request does not contain any extended headers, this function allocates memory for them.
416
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_delete_header#
sl_status_t sl_http_client_delete_header (sl_http_client_request_t * request, const char * key)
Deletes a specified header field from the extended headers of an HTTP client request.
[in] | request | Pointer to an sl_http_client_request_t structure represents the HTTP client request configuration. Must not be NULL. |
[in] | key | Pointer to a string contains the header key to be deleted. Must not be NULL. |
This function removes a header field from the extended headers of the specified HTTP client request based on the provided key.
sl_http_client_add_header should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to delete the header from the request.
441
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_delete_all_headers#
sl_status_t sl_http_client_delete_all_headers (sl_http_client_request_t * request)
Deletes all headers from the extended headers of an HTTP client request.
[in] | request | Pointer to an sl_http_client_request_t structure representing the HTTP client request configuration. Must not be NULL. |
This function removes all headers from the extended headers of the specified HTTP client request. It frees up the memory allocated for these headers.
sl_http_client_add_header should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: The provided request handle is NULL or invalid.
SL_STATUS_FAIL: Failed to delete the headers from the request.
Note
The user must call this function to free the memory allocated for the headers.
466
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_send_request#
sl_status_t sl_http_client_send_request (const sl_http_client_t * client, const sl_http_client_request_t * request)
Sends an HTTP request.
[in] | client | Pointer to an sl_http_client_t object representing the HTTP client handle. Must not be NULL. |
[in] | request | Pointer to an sl_http_client_request_t object representing the HTTP client request configuration. Must not be NULL. |
This function sends an HTTP request using the specified client and request configuration. It must be called after initializing the request with sl_http_client_request_init.
sl_http_client_request_init should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to send the HTTP request.
Note
HTTP HEAD and DELETE methods are not supported on Si91x specific chipsets.
The
body_length
header in the request is set internally by default on Si91x specific chipsets.HTTP PUT does not support sending the body through this API; it is mandatory to call sl_http_client_write_chunked_data on Si91x specific chipsets.
HTTP response status and response codes (e.g., 200, 201, 404) would be returned in the corresponding event handler registered during sl_http_client_request_init.
498
of file components/service/http_client/inc/sl_http_client.h
sl_http_client_write_chunked_data#
sl_status_t sl_http_client_write_chunked_data (const sl_http_client_t * client, const uint8_t * data, uint32_t data_length, bool flush_now)
Sends HTTP POST and PUT chunked data.
[in] | client | Pointer to an sl_http_client_t object representing the HTTP client handle. Must not be NULL. |
[in] | data | Pointer to the buffer containing the data to be written. Must not be NULL. |
[in] | data_length | Length of the data chunk to be sent. |
[in] | flush_now | Boolean flag indicating whether to flush the data immediately. Note that this feature is not supported on Si91x specific chipsets. |
This function sends a chunk of data as part of an HTTP POST or PUT request. It should be used after initiating the request with sl_http_client_send_request. The data can be sent in multiple chunks by calling this function multiple times.
sl_http_client_send_request should be called before this function.
Returns
sl_status_t - Status of the operation. For more details, see https://docs.silabs.com/gecko-platform/latest/platform-common/status.
SL_STATUS_OK: Operation successful.
SL_STATUS_INVALID_PARAMETER: One or more input parameters are NULL or invalid.
SL_STATUS_FAIL: Failed to send the data chunk.
Note
The
flush_now
feature is not supported on Si91x specific chipsets.
533
of file components/service/http_client/inc/sl_http_client.h