How to get a value from client using uhttpd?

Hello everyone!

I am beginning from scratch to learn how to make a web server using uhttpd.

I achieved to run uhttpd. It displays a static page 'index.html' located in my folder web_server/ as I get connected to my router (IP:Port).

I would like now to be able to write data in a field of the page, and store that data in a file in the router. How can I do that?

Solutions on the web are not clear to me until now, it seems to need an additional page with lua code.

A short explanation on system 'architecture' and good practice would be a great help!

Thank you in advance!

HTML pages do nothing on the server, they are just sent to the client, and the browser interprets them.

Your page needs a FORM, that sends the info to a program (not a page) on the server. That program will receive the info from the form, write the data to disk, and return a result page to the client.

Ok, thank you!

I understand that I can add <form action='/mypage' method='get'> or post in html page to obtain data from user.

With GET, data will appear inside the url, in the HTTP request (in the HTTP body with POST).

I also understand that the parsing program to extract the data from the HTTP request is called by uhttpd, and specified in /etc/config/uhttpd config file:

config uhttpd main
        option listen_http '8080'
        option listen_https '4430'
        option enable_http '1'
        option enable_https '1'
        option home '/www'
        option rfc1918_filter '1'
        option max_requests '10'
        option max_connections '100'
        option cert '/etc/uhttpd.crt'
        option key '/etc/uhttpd.key'
        option cgi_prefix '/cgi-bin'
        option script_timeout '600'
        option network_timeout '30'
        option http_keepalive '0'
        option tcp_keepalive '5'
        option ubus_prefix '/ubus'
        option no_dirlists '1'
        option _httpWanAccess '0'
        option _httpsWanAccess '0'
        list lua_prefix '/api=/www/cgi-bin/api_dispatcher.lua'
        option index_page '/myserver/index.html'
        option error_page '/myserver/index.html'

I also understand that uhttpd calls a CGI script, that calls a more complex program (to generate page, to treat data...). Or the CGI can be complex enough to do all treatment tasks. CGI can be written in lua.

Is /www/cgi-bin/api_dispatcher.lua this CGI complex program?

Should I specify list lua_prefix '/api=/myserver/myprog.lua'?

Now, I have written 2 html pages. The first one is static, and has a link (using < href=...>) to my second page. This second page must be able to get data from client and write them to disk.
So CGI should react only for this second page, is it possible?

=> I need to write a GCI script in lua called myprog.lua and set its location in uhttpd config file; myprog will parse HTTP requests and in case of data, write to disk, otherwise just print my first static page.

Isn't it?

Thank you again for your advice!

Note: I tried to follow this topic.

No success, when I access uhttpd (IP:port), I get this page:


Scripts are not run.

Do you I have an advice?

Thank you in advance!