lasselukkari / aWOT

Arduino web server library.
MIT License
292 stars 42 forks source link

Multiple query parameters #13

Closed soccou closed 6 years ago

soccou commented 6 years ago

Hi,

First thanks for sharing your excellent library. I had spent quite a while looking for something like this.

However, it appears that there may be an issue with trying to pass more than one query parameter? or perhaps I am doing something wrong. (that is always very possible... )

Example code

 app.get("/c", &correctionGetRequest);
.
.
.
void correctionGetRequest(Request &req, Response &res) {
  char * correctionValue = req.query("VAL");
  char * chanNumberParam = req.query("ID"); 
  int chanNumber = atoi(chanNumberParam);

  res.success("text/html");

  res.print("chanNumber: ");
  res.print(chanNumber);    
  res.print("<br/>");
  res.print("correctionValue:");
  res.print(correctionValue);   
  res.print("<br/>");

It seems like when you look at the values they both equal value passed for "ID" unless you don't pass that value at all, then the other parameter works, examples :

/c?ID=1&VAL=2.63"
chanNumber: 1<br/>correctionValue:1<br/>

> curl "http://192.168.1.147/c?ID=2&VAL=2.63"
chanNumber: 2<br/>correctionValue:2<br/>

> curl "http://192.168.1.147/c?ID=3&VAL=2.63"
chanNumber: 3<br/>correctionValue:3<br/>

> curl "http://192.168.1.147/c?VAL=2.63"
chanNumber: 0<br/>correctionValue:2.63<br/>

> curl "http://192.168.1.147/c?VAL=2.63&ID=1"
chanNumber: 1<br/>correctionValue:1<br/>

> curl "http://192.168.1.147/c?VAL=2.63&ID=2"
chanNumber: 2<br/>correctionValue:2<br/>

Any suggestions?

lasselukkari commented 6 years ago

Hello! Sorry for the delayed reply.

The existing api to read query parameters maybe wasn't the most intuitive. It was reading the value to an internal buffer and the buffer was overwritten as soon the next value was read.

I updated the function so that the buffer for the value is given as a parameter. These changes were merged in pr https://github.com/lasselukkari/aWOT/pull/14

Here is an updated version of your code.

void correctionGetRequest(Request &req, Response &res) {
  char correctionValue[64];
  char chanNumberParam[64];

  req.query("VAL", correctionValue, 64);
  req.query("ID", chanNumberParam, 64);
  int chanNumber = atoi(chanNumberParam);

  res.success("text/html");
  res.print("chanNumber: ");
  res.print(chanNumber);  
  res.print("<br/>");
  res.print("correctionValue:");
  res.print(correctionValue); 
  res.print("<br/>");
}
soccou commented 6 years ago

Nice, thanks for the reply and the patch. I will give it a try.