-->
Page 1 of 1

Post request ?

PostPosted: Mon Jan 17, 2022 5:50 am
by lisztfr
Hi guys,

In short, I would like to send 2 variables to a server, the Asynchronous web server (hosted on a Nodemcu), in order update the page (and save battery life on the unit which checks the DHT22)

But how do i sent the actual values of a variable and not only the name ?

With JSON or url-encoded, what ever it takes.

In my code i have some sections, first including libraries, defining constants, then a rawlitteral section which contains a index page and a script, then a setup section which starts the server, and a loop section which reads the temperature and humidity. Anyway, i'm a bit lost. I can only do Basic programming, now we have a amazing piece of hardware but so much to learn :-)

I can post the code, it's from a kit bought on amz.

Thanks,

Lh

Re: Post request ?

PostPosted: Tue Jan 18, 2022 6:24 am
by quackmore
But how do i sent the actual values of a variable and not only the name ?


something like this should work
didn't test it
just an example, you'll have to work on it according to your setup
I'd prefer json

using JSON and assuming that variables are strings
Code: Select allchar *request_fmt =
   "POST /your_url HTTP/1.0\r\n"
   "... your other headers here..."
   "Content-Type: application/json\r\n"
   "Content-Length: %d\r\n"
   "\r\n"
   "{\"first_variable_name\":\"%s\",\"second_variable_name\":\"%s\"}"
   "\r\n\r\n";

...
char* request[...];
int body_len = strlen("{\"first_variable_name\":\"\",\"second_variable_name\":\"\"}") +
                         strlen(first_variable) +
                         strlen(second_variable) +

sprintf(request, request_fmt, body_len, first_variable, second_variable);
...


using url encoded parameters and assuming that variables are strings
Code: Select allchar *request_fmt =
   "POST /your_url HTTP/1.0\r\n"
   "... your other headers here..."
   "Content-Type: application/x-www-form-urlencoded\r\n"
   "Content-Length: %d\r\n"
   "\r\n"
   "first_variable_name=%s&second_variable_name=%s"
   "\r\n\r\n";

...
char* request[...];
int body_len = strlen("first_variable_name=&second_variable_name=") +
                         strlen(first_variable_len) +
                         strlen(second_variable_len) +

sprintf(request, request_fmt, body_len, first_variable, second_variable);
...

Re: Post request ?

PostPosted: Sat Jan 22, 2022 9:02 am
by lisztfr
I understand that i have to learn Javascript....