Help with web server end points please

orbitcoms
Posts: 141
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Help with web server end points please

Postby orbitcoms » Sat Apr 17, 2021 9:44 am

Hi,

I have patched together a project from some online tutorials for setting up a web server in my ESP32 (WROOM).
The server works and I get index.html served from the root uri and can open another page (config-id.html) from a link on index.html.
The config page has 2 input boxes and a submit button. When I submit I get an error "Header fields are too long for server to interpret".

Using development window in browser, I see the correct uri being posted to as below...

My html code and web end point code appears below this

Thanks in advance for any help.

Request URL: http://192.168.1.108/setid
Request Method: POST
Status Code: 431 Request Header Fields Too Large
Remote Address: 192.168.1.108:80
Referrer Policy: strict-origin-when-cross-origin
Content-Length: 50
Content-Type: text/html
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 28
Content-Type: application/x-www-form-urlencoded
DNT: 1
Host: 192.168.1.108
Origin: http://192.168.1.108
Pragma: no-cache
Referer: http://192.168.1.108/config-id.html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.39
password: abcd
device_id: 1234

Code: Select all

static esp_err_t on_setid(httpd_req_t *req)
{
    ESP_LOGI(TAG,"ID route hit");
    ESP_LOGI(TAG,"url %s was hit",req->uri);
    char buf[150];
    memset(&buf,0,sizeof(buf));
    httpd_req_recv(req,buf,req->content_len);
    //"password=XXXX\r\ndevice_id=XXXX\r\n"
    char *password = strtok(buf,"\r\n");  //"password=xxxx\0"
    char *device_id = strtok(NULL,"\r\n"); //"device_id=XXXX\0"
    password = strchr(password,'=') + 1; //"XXXX\0" (points to position after "=")
    device_id = strchr(device_id,'=') + 1; //"XXXX\0"
    ESP_LOGI(TAG,"Password %s, ID %s",password,device_id);
//if password == admin then save
    nvs_flash_init();
    nvs_handle_t nvs;
    nvs_open("Config_Data",NVS_READWRITE, &nvs);
    nvs_set_str(nvs,"device_id",device_id);
    nvs_close(nvs);

    httpd_resp_set_status(req,"303");
    httpd_resp_set_hdr(req,"Location","/config-id.html"); //return result to this html file
    httpd_resp_send(req,NULL,0);
    return ESP_OK;
void RegisterEndPoints(void)
{
    httpd_handle_t server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    config.uri_match_fn = httpd_uri_match_wildcard;

    ESP_LOGI(TAG,"Starting Web Server");
    if(httpd_start(&server,&config) != ESP_OK)
    {
        ESP_LOGE(TAG,"Could not start web server");
    }

    httpd_uri_t setid_end_point_config = 
    {
        .uri = "/setid",
        .method = HTTP_POST,
        .handler = on_setid
    };
    httpd_register_uri_handler(server,&setid_end_point_config);

    httpd_uri_t root_end_point_config = 
    {
        .uri = "/*",
        .method = HTTP_GET,
        .handler = on_root
    };
    httpd_register_uri_handler(server,&root_end_point_config);
}
}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/style.css">
<title>Set Serial Number</title>
</head>
<body>
<!-- "action" is url to submit response -->
<form method="POST" action="/setid">
<label for="password">Password</label>
<input type="password" name = "password" id="password" maxlength="10" required><br><br>
<label for="device_id">Cab Serial Number</label>
<input type="number" min="0" step="1" max="65535" id="device_id" name="device_id" value="" required><br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>

Code: Select all



chegewara
Posts: 2174
Joined: Wed Jun 14, 2017 9:00 pm

Re: Help with web server end points please

Postby chegewara » Sat Apr 17, 2021 10:35 am

This is often happening when you want to open web page from smartphone. You can increase header length in menuconfig.

orbitcoms
Posts: 141
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Re: Help with web server end points please

Postby orbitcoms » Sat Apr 17, 2021 10:48 am

Thanks

I increased "Max HTTP Request Header Length" in menu config to 1024 (was set at 512) but now my ESP32 keeps resetting when attempting to handle the route. Below is what happens before reset after route handler called.

Now route handling is working

Who is online

Users browsing this forum: ESP_Sprite, Majestic-12 [Bot] and 60 guests