Alios things 3.3.0: http component introduction

summary

HTTP(HyperText Transfer Protocol) is an application layer protocol used to transfer hypertext. The network protocol stack of AliOS Things includes HTTP components and provides HTTP client standard capabilities. Developers can quickly realize data interaction with the server through HTTP GET, POST and other methods on the device side through component API. For example, the device side initiates an HTTP GET request https://www.aliyun.com/ , get web page data.

This component supports the following functions:

  • HTTP GET
  • HTTP HEAD
  • HTTP POST
  • HTTP PUT

Copyright information

Apache 2.0 License

directory structure

.
├── include
│   └── httpclient.h          # External header file
├── internal                  # Internal header file
├── package.yaml              # Compile configuration file
└── src
    ├── http_aos_wrapper.c    # http transceiver adaptation layer
    ├── http_client.c         # http core processing layer
    ├── http_formdata.c       # http form data processing
    └── http_method_api.c     # http method interface

Dependent component

  • osal_aos
  • mbedtls

Common configuration

The common configuration file of http is in package Yaml

CONFIG_HTTP_SECURE: whether to enable HTTPS support. It is enabled by default

CONFIG_HTTP_SECURE: 1

CONFIG_HTTP_FILE_OPERATE: whether to use file operation. It is closed by default

CONFIG_HTTP_FILE_OPERATE: 0

The internal configuration of http is in the internal / http file_ opts. Within H

HTTPCLIENT_AUTHB_SIZE: length of http authentication data (user name, password)

#ifndef HTTPCLIENT_AUTHB_SIZE
#define HTTPCLIENT_AUTHB_SIZE     128
#endif

HTTPCLIENT_CHUNK_SIZE: http chunk data size

#ifndef HTTPCLIENT_CHUNK_SIZE
#define HTTPCLIENT_CHUNK_SIZE     1024
#endif

HTTPCLIENT_SEND_BUF_SIZE: http send cache size

#ifndef HTTPCLIENT_SEND_BUF_SIZE
#define HTTPCLIENT_SEND_BUF_SIZE  512
#endif

HTTPCLIENT_MAX_HOST_LEN: maximum length of host segment in URL

#ifndef HTTPCLIENT_MAX_HOST_LEN
#define HTTPCLIENT_MAX_HOST_LEN   64
#endif

HTTPCLIENT_MAX_URL_LEN: maximum length of URL

#ifndef HTTPCLIENT_MAX_URL_LEN
#define HTTPCLIENT_MAX_URL_LEN    512
#endif

HTTPCLIENT_MAX_RECV_WAIT_MS: receive timeout

#ifndef HTTPCLIENT_MAX_RECV_WAIT_MS
#define HTTPCLIENT_MAX_RECV_WAIT_MS 5000
#endif

HTTP_PORT: http default port

#ifndef HTTP_PORT
#define HTTP_PORT   80
#endif

HTTPS_PORT: HTTPS default port

#ifndef HTTPS_PORT
#define HTTPS_PORT  443
#endif

API description

Allocate HTTP request header cache and response cache

HTTPC_RESULT httpclient_prepare(httpclient_data_t *client_data, int header_size, int resp_size)
argsdescription
client_dataUser data structure pointer
header_sizeHead size
resp_sizeReply size

Free HTTP request header cache and response cache

HTTPC_RESULT httpclient_unprepare(httpclient_data_t *client_data);
argsdescription
client_dataUser data structure pointer

Reset HTTP request header cache and response cache

void httpclient_reset(httpclient_data_t *client_data)
argsdescription
client_dataUser data structure pointer

Send a GET request to the URL and wait for a reply.

HTTPC_RESULT httpclient_get(httpclient_t *client, char *url, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address
client_dataUser data structure pointer

Send a POST request to the URL and wait for a reply.

HTTPC_RESULT httpclient_post(httpclient_t *client, char *url, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address
client_dataUser data structure pointer

Send a PUT request to the URL and wait for a reply.

HTTPC_RESULT httpclient_put(httpclient_t *client, char *url, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address
client_dataUser data structure pointer

Send a DELETE request to the URL and wait for a reply.

HTTPC_RESULT httpclient_delete(httpclient_t *client, char *url, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address
client_dataUser data structure pointer

Establish connection

HTTPC_RESULT httpclient_conn(httpclient_t *client, const char *url)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address

Send request

HTTPC_RESULT httpclient_send(httpclient_t *client, const char *url, int method, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
urlURL address
methodRequest method. Refer to enumeration HTTP for specific values_ REQUEST_ TYPE
client_dataUser data structure pointer

Receive response

HTTPC_RESULT httpclient_recv(httpclient_t *client, httpclient_data_t *client_data)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
client_dataUser data structure pointer

Close connection

void httpclient_clse(httpclient_t *client)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc

Set request custom header

void httpclient_set_custom_header(httpclient_t *client, char *header)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc
headerCustom header

Get response code

int httpclient_get_response_code(httpclient_t *client)
argsdescription
clientHTTP client context, including configuration parameters, such as service port number, server certificate, etc

Get response header field value

int httpclient_get_response_header_value(char *header_buf, char *name, int *val_pos, int *val_len)
argsdescription
header_bufResponse header cache
nameField name
val_posField start position
val_lenField length

Add text form data

int httpclient_formdata_addtext(httpclient_data_t* client_data, char* content_disposition, char* content_type, char* name, char* data, int data_len);
argsdescription
client_dataUser field pointer
content_dispositionAddress of content to be added
content_typeContent type
nameAddress of name
dataForm data address
data_lenForm data length

Add file form data

int httpclient_formdata_addfile(httpclient_data_t* client_data, char* content_disposition, char* name, char* content_type, char* file_path);
argsdescription
client_dataUser field pointer
content_dispositionAddress of content to be added
nameAddress of name
content_typeContent type
file_pathFile path

Use example

The code download, compilation and firmware burning related to the component use examples all rely on Alios studio, the supporting development tool of AliOS Things, so you need to refer to it first AOS studio instructions for building a development environment Download and install Alios studio.
After the development environment is built, you can test the example according to the following steps.

Step 1 create or open a project

Open existing project

If the case project for testing already exists, you can refer to it AOS studio operating instructions opening project Open an existing project.

Create a new project

The sample code of the component can be run by compiling any solution linked to AliOS Things. Here, choose helloworld_demo case. helloworld_ The source code related to the demo case can be downloaded for reference AOS studio instruction creation project.

Step 2 add components

After downloading the case, you need to download it in HelloWorld_ Package. Of demo component Add dependency on components in yaml:

depends:
  - netmgr: dev_aos   # helloworld_ The netmgr component is introduced into the demo for WiFi networking
  - http: dev_aos     # helloworld_ Introducing http components into demo

Step 3 download components

In the development environment toolbar with Alios studio installed, select terminal - > new terminal to start the terminal, and the default working path is the workspace of the current project. At this time, enter in the command line of the terminal:

aos install http

After the above command is executed successfully, the component source code is downloaded to/ components/http path.

Step 4 add example

In the package of http component Add in yaml Example example code:

source_file:
  - "src/*.c"
  - "example/http_example.c" # add http_example.c

Step 5 compile firmware

After the sample code has been added to the configuration file of the component, and HelloWorld_ After the demo has added a dependency on this component, you can compile helloworld_demo case to generate firmware. Please refer to the specific compilation method AOS studio instructions for compiling firmware.

Step 6 burn firmware

helloworld_ After the firmware of demo case is generated, you can refer to AOS studio instructions for burning firmware To burn firmware.

Step 7 open the serial port

After the firmware is burned, you can view the operation results of the example through the serial port. For the specific method of opening the serial port, please refer to Viewing the log of AOS studio.

After the serial port terminal is opened successfully, you can enter help in the serial port to view the added test commands.

Step 8 http test example

CLI command line input initialization netmgr command:

netmgr_example

CLI command line input networking command:

netmgr -t wifi -c ssid password

Then, from the CLI command line, enter:

http_example

Key log

CLI log:

http comp test success!

Developer Support

HaaS official: https://haas.iot.aliyun.com/
HaaS technology community: https://blog.csdn.net/HaaSTech
The developers' nail group and official account are shown below, and developers' staple groups have technical support students on duty every day.

Keywords: Embedded system IoT http

Added by Toxinhead on Thu, 17 Feb 2022 20:45:43 +0200