Internet Middleware API Reference  Rev 1.0
HttpClient

HttpClient API implements the client-side of HTTP/1.1. More...

Overview

HttpClient API implements the client-side of HTTP/1.1.

It provides base interfaces to execute an HTTP request on a given URL. It also supports HTTPS (HTTP over TLS) to provide secure communication.

How to use this module

In this release, MediaTek provides two types of APIs: high level APIs and low level APIs.

Functions

HTTPCLIENT_RESULT httpclient_get (httpclient_t *client, char *url, httpclient_data_t *client_data)
 This function executes a GET request on a given URL. More...
 
HTTPCLIENT_RESULT httpclient_post (httpclient_t *client, char *url, httpclient_data_t *client_data)
 This function executes a POST request on a given URL. More...
 
HTTPCLIENT_RESULT httpclient_put (httpclient_t *client, char *url, httpclient_data_t *client_data)
 This function executes a PUT request on a given URL. More...
 
HTTPCLIENT_RESULT httpclient_delete (httpclient_t *client, char *url, httpclient_data_t *client_data)
 This function executes a DELETE request on a given URL. More...
 
HTTPCLIENT_RESULT httpclient_connect (httpclient_t *client, char *url)
 This function establishes an HTTP connection with the remote server by given URL. More...
 
HTTPCLIENT_RESULT httpclient_send_request (httpclient_t *client, char *url, int method, httpclient_data_t *client_data)
 This function sends an HTTP(GET or POST) request to the given URL. More...
 
HTTPCLIENT_RESULT httpclient_recv_response (httpclient_t *client, httpclient_data_t *client_data)
 This function receives the response from a server for the last request. More...
 
void httpclient_close (httpclient_t *client)
 This function closes the HTTP connection. More...
 
int httpclient_get_response_code (httpclient_t *client)
 This function gets the HTTP response code assigned to the last request. More...
 
void httpclient_set_custom_header (httpclient_t *client, char *header)
 This function sets a custom header. More...
 
int httpclient_get_response_header_value (char *header_buf, char *name, int *val_pos, int *val_len)
 This function get specified response header value. More...
 

Modules

 Define
 
 Enum
 
 Struct
 

Function Documentation

void httpclient_close ( httpclient_t client)

This function closes the HTTP connection.

Parameters
[in]clientis a pointer to the httpclient_t.
HTTPCLIENT_RESULT httpclient_connect ( httpclient_t client,
char *  url 
)

This function establishes an HTTP connection with the remote server by given URL.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
Returns
Please refer to HTTPCLIENT_RESULT.
HTTPCLIENT_RESULT httpclient_delete ( httpclient_t client,
char *  url,
httpclient_data_t client_data 
)

This function executes a DELETE request on a given URL.

It blocks until completion.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
[in,out]client_datais a pointer to the httpclient_data_t instance to collect the data returned by the request.
Returns
Please refer to HTTPCLIENT_RESULT.
HttpClient Delete Example
1 char *url = "https://ec2-52-76-74-57.ap-southeast-1.compute.amazonaws.com/mcs/test/ok/200";
2 httpclient_t client = {0};
3 httpclient_data_t client_data = {0};
4 char *buf = NULL;
5 buf = pvPortMalloc(BUF_SIZE);
6 if (buf == NULL) {
7  printf("Malloc failed.\r\n");
8  return;
9 }
10 memset(buf, 0, sizeof(buf));
11 client_data.response_buf = buf; //Sets a buffer to store the result.
12 client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
13 httpclient_delete(&client, url, &client_data);
14 printf("Data received: %s\r\n", client_data.response_buf);
HTTPCLIENT_RESULT httpclient_get ( httpclient_t client,
char *  url,
httpclient_data_t client_data 
)

This function executes a GET request on a given URL.

It blocks until completion.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
[in,out]client_datais a pointer to the httpclient_data_t instance to collect the data returned by the request.
Returns
Please refer to HTTPCLIENT_RESULT.
HttpClient Get Example
1 char *url = "https://www.baidu.com/";
2 httpclient_t client = {0};
3 httpclient_data_t client_data = {0};
4 char *buf = NULL;
5 buf = pvPortMalloc(BUF_SIZE);
6 if (buf == NULL) {
7  printf("Malloc failed.\r\n");
8  return;
9 }
10 memset(buf, 0, sizeof(buf));
11 client_data.response_buf = buf; //Sets a buffer to store the result.
12 client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
13 httpclient_get(&client, url, &client_data);
14 printf("Data received: %s\r\n", client_data.response_buf);
int httpclient_get_response_code ( httpclient_t client)

This function gets the HTTP response code assigned to the last request.

Parameters
[in]clientis a pointer to the httpclient_t.
Returns
The HTTP response code of the last request.
int httpclient_get_response_header_value ( char *  header_buf,
char *  name,
int *  val_pos,
int *  val_len 
)

This function get specified response header value.

Parameters
[in]header_bufis the response header buffer.
[in]nameis the specified http response header name.
HTTPCLIENT_RESULT httpclient_post ( httpclient_t client,
char *  url,
httpclient_data_t client_data 
)

This function executes a POST request on a given URL.

It blocks until completion.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
[in,out]client_datais a pointer to the httpclient_data_t instance to collect the data returned by the request. It also contains the data to be posted.
Returns
Please refer to HTTPCLIENT_RESULT.
HttpClient Post Example
1 char *url = "https://api.mediatek.com/mcs/v2/devices/D0n2yhrl/datapoints.csv";
2 char *header = "deviceKey:FZoo0S07CpwUHcrt\r\n";
3 char *content_type = "text/csv";
4 char *post_data = "1,,I am string!";
5 httpclient_t client = {0};
6 httpclient_data_t client_data = {0};
7 char *buf = NULL;
8 buf = pvPortMalloc(BUF_SIZE);
9 if (buf == NULL) {
10  printf("Malloc failed.\r\n");
11  return;
12 }
13 memset(buf, 0, sizeof(buf));
14 client_data.response_buf = buf; //Sets a buffer to store the result.
15 client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
16 httpclient_set_custom_header(&client, header); //Sets the custom header if needed.
17 client_data.post_buf = post_data; //Sets the user data to be posted.
18 client_data.post_buf_len = strlen(post_data); //Sets the post data length.
19 client_data.post_content_type = content_type; //Sets the content type.
20 httpclient_post(&client, url, &client_data);
21 printf("Data received: %s\r\n", client_data.response_buf);
HTTPCLIENT_RESULT httpclient_put ( httpclient_t client,
char *  url,
httpclient_data_t client_data 
)

This function executes a PUT request on a given URL.

It blocks until completion.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
[in,out]client_datais a pointer to the httpclient_data_t instance to collect the data returned by the request. It also contains the data to be put.
Returns
Please refer to HTTPCLIENT_RESULT.
HttpClient Put Example
1 char *url = "https://ec2-52-76-74-57.ap-southeast-1.compute.amazonaws.com/mcs/test/ok/200";
2 char *content_type = "text/csv";
3 char *put_data = "1,,I am string!";
4 httpclient_t client = {0};
5 httpclient_data_t client_data = {0};
6 char *buf = NULL;
7 buf = pvPortMalloc(BUF_SIZE);
8 if (buf == NULL) {
9  printf("Malloc failed.\r\n");
10  return;
11 }
12 memset(buf, 0, sizeof(buf));
13 client_data.response_buf = buf; //Sets a buffer to store the result.
14 client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
15 client_data.post_buf = put_data; //Sets the user data to be put.
16 client_data.post_buf_len = strlen(put_data); //Sets the put data length.
17 client_data.post_content_type = content_type; //Sets the content type.
18 httpclient_put(&client, url, &client_data);
19 printf("Data received: %s\r\n", client_data.response_buf);
HTTPCLIENT_RESULT httpclient_recv_response ( httpclient_t client,
httpclient_data_t client_data 
)

This function receives the response from a server for the last request.

Parameters
[in]clientis a pointer to the httpclient_t.
[out]client_datais a pointer to the httpclient_data_t instance to collect the data returned by the request.
Returns
Please refer to HTTPCLIENT_RESULT.
HTTPCLIENT_RESULT httpclient_send_request ( httpclient_t client,
char *  url,
int  method,
httpclient_data_t client_data 
)

This function sends an HTTP(GET or POST) request to the given URL.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]urlis the URL to run the request.
[in]methodis HTTPCLIENT_REQUEST_TYPE.
[in]client_datais a pointer to the httpclient_data_t instance to collect the data to be posted.
Returns
Please refer to HTTPCLIENT_RESULT.
void httpclient_set_custom_header ( httpclient_t client,
char *  header 
)

This function sets a custom header.

Parameters
[in]clientis a pointer to the httpclient_t.
[in]headeris a custom header string.
Returns
None.