HTTP Server URI Question

Voltmin
Posts: 6
Joined: Wed Oct 27, 2021 4:10 pm

HTTP Server URI Question

Postby Voltmin » Wed Oct 27, 2021 5:30 pm

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.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: HTTP Server URI Question

Postby ESP_Sprite » 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.

Voltmin
Posts: 6
Joined: Wed Oct 27, 2021 4:10 pm

Re: HTTP Server URI Question

Postby Voltmin » Thu Oct 28, 2021 7:55 pm

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 *?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: HTTP Server URI Question

Postby ESP_Sprite » 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.

Dansen
Posts: 9
Joined: Thu Oct 28, 2021 9:42 am

Re: HTTP Server URI Question

Postby Dansen » Fri Oct 29, 2021 6:39 am

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?

Voltmin
Posts: 6
Joined: Wed Oct 27, 2021 4:10 pm

Re: HTTP Server URI Question

Postby Voltmin » Fri Oct 29, 2021 2:28 pm

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.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: HTTP Server URI Question

Postby ESP_Sprite » 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.

Voltmin
Posts: 6
Joined: Wed Oct 27, 2021 4:10 pm

Re: HTTP Server URI Question

Postby Voltmin » Tue Nov 02, 2021 2:07 pm

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

Voltmin
Posts: 6
Joined: Wed Oct 27, 2021 4:10 pm

Re: HTTP Server URI Question

Postby Voltmin » Mon Nov 29, 2021 8:06 pm

Sorry for the late response, I had to prioritize a different project. The solution works great! I appreciate the help.

Who is online

Users browsing this forum: HighVoltage, icyTwist and 97 guests