Page 1 of 1

HTTP Server URI Question

Posted: Wed Oct 27, 2021 5:30 pm
by Voltmin
Question: Can the ESP-IDF acquire integers from a HTTP URI?

Background: I am currently working on a program in C that replicates some endpoints used by a Raspberry Pi (Python). An example of a Pi URI is something like "/grab/4", where it is registered as "/grab/<int:amount>" in the code and amount can be used no problem. To my understanding, where a request is sent to the ESP, it is only capable of obtaining variables from the header or queries, not as parsable variables in the URI. I'd like to know If anyone has any experience with this or can provide some reasons to why this might be.

Re: HTTP Server URI Question

Posted: Thu Oct 28, 2021 12:53 am
by ESP_Sprite
Depends on the http server you're using. If using esp-httpd (the one included in esp-idf), you could simply register your own get (or post) handler for the "/grab/*" url and parse the url yourself.

Re: HTTP Server URI Question

Posted: Thu Oct 28, 2021 7:55 pm
by Voltmin
ESP_Sprite wrote:
Thu Oct 28, 2021 12:53 am
Depends on the http server you're using. If using esp-httpd (the one included in esp-idf), you could simply register your own get (or post) handler for the "/grab/*" url and parse the url yourself.
How would you obtain whatever argument is input for *?

Re: HTTP Server URI Question

Posted: Fri Oct 29, 2021 5:29 am
by ESP_Sprite
It's essentially a string, so standard C functions will work: sscanf, atoi, strstr etc. You'd use a combination of those.

Re: HTTP Server URI Question

Posted: Fri Oct 29, 2021 6:39 am
by Dansen
Hello!

I'm facing the same problem with the httpd example provided by the esp idf, but i cant find a Method to grab the acutal URL. I found no function, or structure in which the URL ist stored.

You might have some ideas, on how to obtain the URL?

Re: HTTP Server URI Question

Posted: Fri Oct 29, 2021 2:28 pm
by Voltmin
ESP_Sprite wrote:
Fri Oct 29, 2021 5:29 am
It's essentially a string, so standard C functions will work: sscanf, atoi, strstr etc. You'd use a combination of those.
Looks like if you input the string "grab/*" for the .uri when making the http_uri_t variable, the * gets registered as a string, so only get/* can be called, not get/whatever.

Re: HTTP Server URI Question

Posted: Sat Oct 30, 2021 2:06 am
by ESP_Sprite
Voltmin wrote:
Fri Oct 29, 2021 2:28 pm
Looks like if you input the string "grab/*" for the .uri when making the http_uri_t variable, the * gets registered as a string, so only get/* can be called, not get/whatever.
Was wondering about your response because I actually have it working in a bunch of my apps... Turns out it's a configuration option. You need to set 'config.uri_match_fn = httpd_uri_match_wildcard;' in the httpd_config_d struct when you initialize esp_httpd to make wildcards work; the default is a strcmp() that indeed would have the behaviour you describe.

Re: HTTP Server URI Question

Posted: Tue Nov 02, 2021 2:07 pm
by Voltmin
ESP_Sprite wrote:
Sat Oct 30, 2021 2:06 am
Voltmin wrote:
Fri Oct 29, 2021 2:28 pm
Looks like if you input the string "grab/*" for the .uri when making the http_uri_t variable, the * gets registered as a string, so only get/* can be called, not get/whatever.
Was wondering about your response because I actually have it working in a bunch of my apps... Turns out it's a configuration option. You need to set 'config.uri_match_fn = httpd_uri_match_wildcard;' in the httpd_config_d struct when you initialize esp_httpd to make wildcards work; the default is a strcmp() that indeed would have the behaviour you describe.
Ahhhh, thank you. I am currently using HTTPD_SSL_CONFIG_DEFAULT() for the HTTPS server and had no idea what a wildcard was. I will play around with it and see how it goes. :D

Re: HTTP Server URI Question

Posted: Mon Nov 29, 2021 8:06 pm
by Voltmin
Sorry for the late response, I had to prioritize a different project. The solution works great! I appreciate the help.