Adesina Adewale
Adesina Adewale

Reputation: 49

Get request URL in Arduino web server

Am trying to display whatever value is sent to my Arduino (mega + WiFi r3) web server how do I do it ? Thanks in advance.

Using this sample, the server listens for "ledOn" and then performs an action but I want the server to listen to any request coming from clients and display the requests in the serial monitor.

server.on("ledOn", [](){
// My code     
  });

Upvotes: 1

Views: 4080

Answers (2)

Raja
Raja

Reputation: 1259

The uri() function only gives the first part of the URL. It does not give access to all the parameters that follow it. If you know the names of all the parameters sent by the client, you can use code like server.arg("USERNAME") to get the value of the parameter. But there may be situations where you do not know in advance how many parameters will be sent to the server or their names. The following code snippet can handle this situation:

  int numArgs = server.args();
  Serial.print("Number of args: ");
  Serial.println(numArgs);
  for (int i=0; i< numArgs; i++){
    Serial.print(server.argName(i));
    Serial.print(" : ");
    Serial.println(server.arg(i));
  }

The ESP8266WebServer library does all the parsing and gives you each individual parameter. In case you still want the raw URL that the client used, you can reverse the process: Splice the name-value pairs together using = and & as delimiters. For example:

http://example.com/user?name=Adewale&city=Pune

Upvotes: 0

Juraj
Juraj

Reputation: 3736

You use the ESP8266WebServer library in the ESP8266 on the combined board. The reference is in the README file and the library has good examples.

The function to get the request's URL is server.uri().

Usually to process the GET request the URL is not read with uri() function, but the resource part (the 'path') is matched with the on() functions in setup() as server.on("some/path", fncToHandle); and the URL parameters of a GET request are parsed by the WebServer library and made available with a set of functions:

const String & arg();
const String & argName();
int args();
bool hasArg();

the standard url parameters are after ? in form of name=value, separated by & like

 /some/path?name=John&lastName=Smith

snippets from SimpleAuthentication example:

from setup()

server.on("/login", handleLogin);

from handleLogin:

  if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")) {
    if (server.arg("USERNAME") == "admin" &&  server.arg("PASSWORD") == "admin") {

Upvotes: 1

Related Questions