This library provides the HTTP Client implementation that can work on the PSoC™ 6 MCU platforms with Wi-Fi connectivity.This library supports RESTful methods such as GET, PUT, POST, HEAD, DELETE, PATCH, CONNECT, OPTIONS and TRACE to communicate with the HTTP Server. This library uses coreHTTP module of AWS IoT Device SDK for Embedded C library. Please refer to AWS IoT Device SDK for Embedded C library's readme for the HTTP protocol versions supported.
Features
- Supports Wi-Fi and Ethernet connections.
- Secure [with TLS security] and non-secure modes of connection.
- Supports RESTful HTTP methods: GET, PUT, POST, HEAD, DELETE, PATCH, CONNECT, OPTIONS and TRACE.
- Handles various resource content types such as HTML, Plain, and JSON.
- Provides synchronous API to send the request and receive the response.
- Provide utility APIs to create HTTP headers for forming the HTTP request. And to parse the HTTP headers received in the response.
- Supports large data downloads through range requests.
AnyCloud State Machine
The HTTP Client library provides C APIs in ModusToolbox™ environment to send HTTP request to the server and receive the response. API state machine to perform HTTP send request operation is given below.
HTTP Client send request
+-----------------------+
| cy_http_client_init() |
+-----------------------+
|
v
+----------------------------+
| cy_http_client_create() |
+----------------------------+
|
v
+--------------------------+
| cy_http_client_connect() |
+--------------------------+
|
v
+------------------------------+
| cy_http_client_write_header()|
+------------------------------+
|
v
+-----------------------+
| cy_http_client_send() |
+-----------------------+
|
v
+-----------------------------+
| cy_http_client_disconnect() |
+-----------------------------+
|
v
+-------------------------+
| cy_http_client_delete() |
+-------------------------+
|
v
+-------------------------+
| cy_http_client_deinit() |
+-------------------------+
Supported Platform
Supported Frameworks
This middleware library supports the following frameworks:
- ModusToolbox™ environment: In this environment the HTTP Client Library uses the abstraction-rtos library that provides the RTOS abstraction API and uses the secure-sockets library for implementing socket functions.
Dependencies
This HTTP Client library depends on the following library:
Quick Start
This library is supported only on ModusToolbox™ environment. The following section provides information on how to build the library in those frameworks.
ModusToolbox™
- To use http-client library with Wi-Fi kits on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull http-client library and wifi-core-freertos-lwip-mbedtls library which will internally pull secure-sockets, wifi-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull wifi-core-freertos-lwip-mbedtls and http-client libraries create the following *.mtb* files in deps folder.
- To use http-client library with CYW955913EVK-01 kit, the application should pull http-client library and wifi-core-threadx-cat5 library which will internally pull secure-sockets, wifi-connection-manager and other dependent modules. To pull wifi-core-threadx-cat5 and http-client libraries create the following *.mtb* files in deps folder.
- To use http-client library with Ethernet kits on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull http-client library and ethernet-core-freertos-lwip-mbedtls library which will internally pull secure-sockets, ethernet-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull ethernet-core-freertos-lwip-mbedtls and http-client libraries create the following *.mtb* files in deps folder.
- Review and make the required changes to the pre-defined configuration files.
- The configuration files are bundled with the wifi-mw-core library for FreeRTOS, lwIP, and Mbed TLS. See README.md for more details.
- If the application is using bundle library then the configuration files are in the bundle library. For example if the application is using Wi-Fi core freertos lwip mbedtls bundle library, the configuration files are in
wifi-core-freertos-lwip-mbedtls/configs folder. Similarly if the application is using Ethernet Core FreeRTOS lwIP mbedtls library, the configuration files are in ethernet-core-freertos-lwip-mbedtls/configs folder.
- Define the following COMPONENTS in the application's Makefile for the HTTP client library. For additional information, see the "Quick Start" section in README.md.
COMPONENTS=FREERTOS MBEDTLS LWIP SECURE_SOCKETS
- Note
- For CYW955913EVK-01 only the following COMPONENTS are required.
COMPONENTS=SECURE_SOCKETS
- HTTP Client Library disables all the debug log messages by default. To enable log messages, the application must perform the following:
- Add the
ENABLE_HTTP_CLIENT_LOGS macro to the DEFINES in the code example's Makefile. The Makefile entry would look as follows: DEFINES+=ENABLE_HTTP_CLIENT_LOGS
- Call the
cy_log_init() function provided by the cy-log module. cy-log is part of the connectivity-utilities library. See connectivity-utilities library API documentation for cy-log details.
- Define the following macro in the application's Makefile to configure the response header maximum length to 'N'. By default, this value will be set to 2048:
DEFINES+=HTTP_MAX_RESPONSE_HEADERS_SIZE_BYTES=<N>
- Define the following macro in the application's Makefile to configure the user agent name in all request headers. By default, this component will be added to the request header:
DEFINES += HTTP_USER_AGENT_VALUE="\"anycloud-http-client\""
- Define the following macro in the application's Makefile to mandatorily disable the custom configuration header file.
DEFINES += HTTP_DO_NOT_USE_CUSTOM_CONFIG
DEFINES += MQTT_DO_NOT_USE_CUSTOM_CONFIG
- The "aws-iot-device-sdk-port" layer includes the "coreHTTP" and "coreMQTT" modules of the "aws-iot-device-sdk-embedded-C" library by default. If the user application doesn't use MQTT client features, add the following path in the .cyignore file of the application to exclude the coreMQTT source files from the build.
$(SEARCH_aws-iot-device-sdk-embedded-C)/libraries/standard/coreMQTT
libs/aws-iot-device-sdk-embedded-C/libraries/standard/coreMQTT
- Enable smart HTTP response buffering to prevent timeouts.
Define the CY_HTTP_CLIENT_ENABLE_SMART_BUFFERING macro in the application's Makefile to enable this feature. By default, this feature is disabled.
DEFINES += CY_HTTP_CLIENT_ENABLE_SMART_BUFFERING
When enabled, this feature optimizes HTTP response handling by:
- First reading a small chunk of the response to analyze the headers
- Determining the exact response size from the Content-Length header
- Requesting the precise response size needed for the complete HTTP response
- Reading the remaining response data without unnecessary waiting
This prevents timeouts that occur when the requested response size is greater than the actual response size, eliminating unnecessary network wait times. Enable this feature to improve response time and reduce network timeouts for HTTP requests where response sizes vary significantly.
- Define the following macro in the application's Makefile to configure the first chunk size 'N' for HTTP client library. By default this value will be set to 128 bytes and will be keep incrementing by 128 bytes until "Content-Length" and header ending byte sequence is found. Note: Ideal case is that the complete HTTP header and header field "Content-Length" be present within this chunk size. If your HTTP responses have large headers, you may need to increase this value accordingly. Keep this value such that it will accomodate the complete header but not complete response ( this should accomodate the complete Header and partial Body ) and should not be more than the total size of http response buffer.
- Ideally this value should be set to size of complete header including terminating byte sequence + 0 or more byte of body but not complete body.
------------------------------------------------------------------------------------
| HTTP Response Header | HTTP Response Body |
------------------------------------------------------------------------------------
-----------------------------↑
CY_HTTP_RESPONSE_FIRST_CHUNK_SIZE
DEFINES += CY_HTTP_RESPONSE_FIRST_CHUNK_SIZE=<N>
Note: The library includes a compile-time sanity check that gives a compile time warning to ensure CY_HTTP_RESPONSE_FIRST_CHUNK_SIZE is at least 128 bytes. If you need to use a smaller value (not recommended), make sure it will have atleast one header field in that.
Warning: First chunk should contain ATLEAST one Header-Field : Value pair in it. Otherwise the app will fail.
Code Snippets
This section provides code snippets for this library on the ModusToolbox™. The code snippets given under the ModusToolbox™ section use C APIs. In general, the library features are tested on ModusToolbox™ using C APIs.
- ModusToolbox™ - Snippets for HTTP Client create, connect, send, disconnect and delete (C implementation).
ModusToolbox™
This code snippet demonstrates the initialization of the configuration structures required for the operation such as creating a secure or non-secure connection and communicating with the HTTP server using the supported methods.
Code Snippet 1: Creation and Initialization of Handle for Non-Secure HTTP Client
Creates an HTTP Client handle for a non-secure HTTP connection using the cy_http_client_create function.*
cy_awsport_server_info_t server_info;
void snippet_http_client_non_secure_create( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_init(void)
Initializes the Http Client library and its components.
cy_rslt_t cy_http_client_create(cy_awsport_ssl_credentials_t *security, cy_awsport_server_info_t *server_info, cy_http_disconnect_callback_t disconn_cb, void *user_data, cy_http_client_t *handle)
Creates a HTTP Client instance and initializes its members based on the input arguments.
void * cy_http_client_t
HTTP Client handle.
Definition: cy_http_client_api.h:212
Code Snippet 2: Creation and Initialization of Handle for Secure HTTP Client
Creates an HTTP Client handle for a secure HTTP connection using the cy_http_client_create function.*
#define SSL_SERVERCERT_PEM \
"-----BEGIN CERTIFICATE-----\n" \
"........base64 data........\n" \
"-----END CERTIFICATE-----"
#define SSL_SERVERKEY_PEM \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"..........base64 data..........\n" \
"-----END RSA PRIVATE KEY-----"
#define SSL_ROOTCA_PEM \
"-----BEGIN CERTIFICATE-----\n" \
"........base64 data........\n" \
"-----END CERTIFICATE-----"
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
{
}
void snippet_http_client_secure_create(void)
{
cy_rslt_t res = CY_RSLT_SUCCESS;
( void ) memset( &credentials, 0, sizeof( credentials ) );
( void ) memset( &server_info, 0, sizeof( server_info ) );
credentials.client_cert = (const char *) &SSL_SERVERCERT_PEM;
credentials.client_cert_size = sizeof( SSL_SERVERCERT_PEM );
credentials.private_key = (const char *) &SSL_SERVERKEY_PEM;
credentials.private_key_size = sizeof( SSL_SERVERKEY_PEM );
credentials.root_ca = (const char *) &SSL_ROOTCA_PEM;
credentials.root_ca_size = sizeof( SSL_ROOTCA_PEM );
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
http_cb = disconn_callback;
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_http_client_disconn_type_t
HTTP Client disconnect type.
Definition: cy_http_client_api.h:201
void(* cy_http_disconnect_callback_t)(cy_http_client_t handle, cy_http_client_disconn_type_t type, void *user_data)
Disconnect notification callback function which was registered while invoking /ref cy_http_client_cre...
Definition: cy_http_client_api.h:224
Code Snippet 3: Establish connection to the given server
Establish connection of http client to given HTTP server using the cy_http_client_connect function.*
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000 )
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
void snippet_http_client_connect( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_connect(cy_http_client_t handle, uint32_t send_timeout_ms, uint32_t receive_timeout_ms)
Connects to the given HTTP server and establishes a connection.
Code Snippet 4: Generation of HTTP request header
Generate HTTP request header with the provided input using the cy_http_client_write_header function.*
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000 )
#define GET_PATH "/get"
#define USER_BUFFER_LENGTH ( 2048 )
uint8_t userBuffer[ USER_BUFFER_LENGTH ];
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
uint32_t num_header;
void snippet_http_client_write_header( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
header[0].
field =
"Connection";
header[0].
value =
"keep-alive";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_write_header(cy_http_client_t handle, cy_http_client_request_header_t *request, cy_http_client_header_t *header, uint32_t num_header)
Generates the request Header used as HTTP Client request header during cy_http_client_send.
@ CY_HTTP_CLIENT_METHOD_GET
HTTP GET Method
Definition: cy_http_client_api.h:186
HTTP structure containing the fields required for response header and body.
Definition: cy_http_client_api.h:262
Code Snippet 5: Send a HTTP request to server
Send a HTTP request (header + body) to the server and receive response by using cy_http_client_send function.*
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000 )
#define GET_PATH "/get"
#define USER_BUFFER_LENGTH ( 2048 )
#define REQUEST_BODY "Hello, world!"
#define REQUEST_BODY_LENGTH ( sizeof( REQUEST_BODY ) - 1 )
uint8_t userBuffer[ USER_BUFFER_LENGTH ];
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
uint32_t num_header;
void snippet_http_client_send( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
header[0].
field =
"Connection";
header[0].
value =
"keep-alive";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
res =
cy_http_client_send(handle, &request, (uint8_t *)REQUEST_BODY, REQUEST_BODY_LENGTH, &response);
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_send(cy_http_client_t handle, cy_http_client_request_header_t *request, uint8_t *payload, uint32_t payload_len, cy_http_client_response_t *response)
Sends the HTTP request to the server and returns the received HTTP response from the server.
Code Snippet 6: Read header from the response
Read HTTP header from the received response using the cy_http_client_read_header function.*
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000 )
#define GET_PATH "/get"
#define USER_BUFFER_LENGTH ( 2048 )
#define REQUEST_BODY "Hello, world!"
#define REQUEST_BODY_LENGTH ( sizeof( REQUEST_BODY ) - 1 )
uint8_t userBuffer[ USER_BUFFER_LENGTH ];
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
uint32_t num_header;
void snippet_http_client_read_header( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
header[0].
field =
"Connection";
header[0].
value =
"keep-alive";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
res =
cy_http_client_send(handle, &request, (uint8_t *)REQUEST_BODY, REQUEST_BODY_LENGTH, &response);
if( res != CY_RSLT_SUCCESS )
{
}
( void ) memset( &header[0], 0, sizeof( header[0] ) );
header[0].
field =
"Connection";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_read_header(cy_http_client_t handle, cy_http_client_response_t *response, cy_http_client_header_t *header, uint32_t num_header)
Parses the headers received in the HTTP response.
Code Snippet 7: Close the connection with server
Disconnect from the HTTP server, and release the HTTP Client library resources by using cy_http_client_disconnect, cy_http_client_delete and cy_http_client_deinit API functions.*
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000 )
#define GET_PATH "/get"
#define USER_BUFFER_LENGTH ( 2048 )
#define REQUEST_BODY "Hello, world!"
#define REQUEST_BODY_LENGTH ( sizeof( REQUEST_BODY ) - 1 )
uint8_t userBuffer[ USER_BUFFER_LENGTH ];
cy_awsport_ssl_credentials_t credentials;
cy_awsport_server_info_t server_info;
uint32_t num_header;
void snippet_http_client_teardown( void )
{
cy_rslt_t res = CY_RSLT_SUCCESS;
server_info.host_name = SNIPPET_SERVER_HOST;
server_info.port = SNIPPET_SERVER_NON_SECURE_PORT;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
header[0].
field =
"Connection";
header[0].
value =
"keep-alive";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
res =
cy_http_client_send(handle, &request, (uint8_t *)REQUEST_BODY, REQUEST_BODY_LENGTH, &response);
if( res != CY_RSLT_SUCCESS )
{
}
( void ) memset( &header[0], 0, sizeof( header[0] ) );
header[0].
field =
"Connection";
num_header = 1;
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
if( res != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_client_deinit(void)
De-initializes the global resources used by the HTTP Client library.
cy_rslt_t cy_http_client_disconnect(cy_http_client_t handle)
Disconnects the HTTP Client network connection.
cy_rslt_t cy_http_client_delete(cy_http_client_t handle)
Deletes the HTTP Client library Object.