1# Simple HTTPD Server Example
2
3The Example consists of HTTPD server demo with demostration of URI handling :
4    1. URI \hello for GET command returns "Hello World!" message
5    2. URI \echo for POST command echoes back the POSTed message
6
7## How to use example
8
9### Hardware Required
10
11* A development board with ESP32/ESP32-S2/ESP32-C3 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
12* A USB cable for power supply and programming
13
14### Configure the project
15
16```
17idf.py menuconfig
18```
19* Open the project configuration menu (`idf.py menuconfig`) to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details.
20
21### Build and Flash
22
23Build the project and flash it to the board, then run monitor tool to view serial output:
24
25```
26idf.py -p PORT flash monitor
27```
28
29(Replace PORT with the name of the serial port to use.)
30
31(To exit the serial monitor, type ``Ctrl-]``.)
32
33See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
34
35### Test the example :
36        * run the test script : "python scripts/client.py \<IP\> \<port\> \<MSG\>"
37            * the provided test script first does a GET \hello and displays the response
38            * the script does a POST to \echo with the user input \<MSG\> and displays the response
39        * or use curl (asssuming IP is 192.168.43.130):
40            1. "curl 192.168.43.130:80/hello"  - tests the GET "\hello" handler
41            2. "curl -X POST --data-binary @anyfile 192.168.43.130:80/echo > tmpfile"
42                * "anyfile" is the file being sent as request body and "tmpfile" is where the body of the response is saved
43                * since the server echoes back the request body, the two files should be same, as can be confirmed using : "cmp anyfile tmpfile"
44            3. "curl -X PUT -d "0" 192.168.43.130:80/ctrl" - disable /hello and /echo handlers
45            4. "curl -X PUT -d "1" 192.168.43.130:80/ctrl" -  enable /hello and /echo handlers
46
47## Example Output
48```
49I (9580) example_connect: - IPv4 address: 192.168.194.219
50I (9580) example_connect: - IPv6 address: fe80:0000:0000:0000:266f:28ff:fe80:2c74, type: ESP_IP6_ADDR_IS_LINK_LOCAL
51I (9590) example: Starting server on port: '80'
52I (9600) example: Registering URI handlers
53I (66450) example: Found header => Host: 192.168.194.219
54I (66460) example: Request headers lost
55```
56
57## Troubleshooting
58* If the server log shows "httpd_parse: parse_block: request URI/header too long", especially when handling POST requests, then you probably need to increase HTTPD_MAX_REQ_HDR_LEN, which you can find in the project configuration menu (`idf.py menuconfig`): Component config -> HTTP Server -> Max HTTP Request Header Length
59