How to handle <form> data received by http server e.g (key=value&key=value&key=value&…) using C language?

Alsaber
Posts: 2
Joined: Wed Oct 24, 2018 1:00 pm

How to handle <form> data received by http server e.g (key=value&key=value&key=value&…) using C language?

Postby Alsaber » Tue Jan 15, 2019 1:42 am

I have http server running on esp32. It serves a short html web page that contains a form. Once I fill the form and click submit using POST method, I receive the form values as follows:
Key1=value1&Key2=value2&Key3=value3&...
The whole data received is saved as a string inside a buffer.

The question is: how can I handle this data by saving each key=vale in a variable so it it can be later saved in the NVS flash? e.g:

Code: Select all

int key1 = value1
int key2 = value2 
int key3 = value3
I am using ESP-IDF and C language

Thank you very much


User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: How to handle <form> data received by http server e.g (key=value&key=value&key=value&…) using C language?

Postby mbratch » Fri Sep 24, 2021 8:22 pm

It's just a C string parsing exercise, and there are a few different reasonable ways to do it. You could parse the string using strtok to get the key-value pairs. You could then parse the values from the keys using "strtok" again if you wanted to, inside of your loop that processes the pairs. You may have to further convert your value to the type you want it to be if not a string. Once you have a string key and a value, you can call the NVS API to write it.


Who is online

Users browsing this forum: Baidu [Spider] and 355 guests