HTTP Server
Overview
This example demonstrates how to implement a simple HTTP server.
Requirements
A normally connected AP is required.
Configure the WiFi SSID, WiFi Password, and whether to use HTTPS in menuconfig.
Building and Flashing
Example Location: examples/network/httpd_websoecket/http_server_post
compile, burn, and more, see: Quick Start Guide
Specific Steps
The PC and the device should be in the same local area network;
During compilation, configure the WiFi SSID, WiFi Password, and whether to use HTTPS in menuconfig, and then flash it to the device;
After the device starts, it will begin to connect to the WiFi and obtain an IP. When “start httpd on port 80” is printed, it means that the http server has been created.
On the PC side, use a tool to POST data to the Http server. Refer to the following bash command:
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://10.17.1.40:80/jsontest
If you send data to the https server using curl, add the parameter –insecure
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' --insecure https://10.17.1.40:443/jsontest
You can also use software such as Postman to send data. Note that the IP in the above URL should be changed to the actual IP address obtained by the device.
Certificate
When using https, a certificate is required. The self-signed certificate used in the example main.c is generated by the following commands:
generate a cert which signed by itself:
1 Generate ECDSA private key:
openssl ecparam -genkey -name prime256v1 -out server.key
2 Generate Certificate Signing Request (CSR):
openssl req -new -key server.key -out server.csr
3 Generate self signed certificate (99 Years):
openssl x509 -req -in server.csr -signkey server.key -out server.crt -days 36135
In the actual product, replace the certificate in main. c with your own certificate.
Running Result
Upon successful execution, it will output the following logs
[I] (14) example_lws: Start http server example
[I] (1970) wm_nm_wifi: start connecting new(12345678)
[I] (2744) wm_nm_core: new is connected
[I] (4960) wm_nm_core: sta got ip: 10.17.1.40
[I] (4970) example_lws: wifi station start success, network is ready!
[I] (4970) example_lws: start httpd on port 80
[2000/01/01 10:13:33:9700] N: 179344: lws_create_context: LWS: 4.3.3-WM, MbedTLS-3.4.0 NET CLI SRV H1 WS ConMon IPv6-absent
[2000/01/01 10:13:33:9760] N: 178936: mem: platform fd map: 120 bytes
[2000/01/01 10:13:33:9760] N: 178160: __lws_lc_tag: ++ [wsi|0|pipe] (1)
[2000/01/01 10:13:33:9780] N: 177048: __lws_lc_tag: ++ [vh|0|default||80] (1)
[2000/01/01 10:13:33:9790] N: 176504: [vh|0|default||80]: lws_socket_bind: source ads 0.0.0.0
[2000/01/01 10:13:33:9800] N: 175872: __lws_lc_tag: ++ [wsi|1|listen|default||80] (2)
[2000/01/01 10:14:01:5360] N: 174632: __lws_lc_tag: ++ [wsisrv|0|adopted] (1)
[I] (32547) example_lws: LWS_CALLBACK_HTTP
[I] (32547) example_lws: LWS_CALLBACK_HTTP_BODY
[I] (32548) example_lws: recv body=
{"key1":"value1", "key2":"value2"}
[I] (32548) example_lws: LWS_CALLBACK_HTTP_BODY_COMPLETION
[I] (32549) example_lws: response:
{"status": "success", "message": "Received your request."}
[2000/01/01 10:14:01:5660] N: 175056: __lws_lc_untag: -- [wsisrv|0|adopted] (0) 30.000ms