Reference Home

Get Request

Description

Get request is a complex function that automates management of the request arrived to our Arduino WEB server. It must be placed in the loop Arduino function for requests polling.

Each time a request comes in, the function selects the routine that has to be activated.

Afterwards function returns the resource name (URL) requested.

Otherwise do noting and returns NULL.

The match, between resource (URL) requested and routine, is done using an array of WEBRES typedef. Each WEBRES is a struct of two fields : resource name, routine name.

Ex.: WEBRES ws={"/index",mymenu} // where menu is the function mymenu()

So an array of WEBRES looks like this:

WEBRES rs[7]={{"/index",pindex},{"/Analog",panalog},{"/RDigital",rdigital},
             {"/WDigital",wdigital},{"/wdig",wdig},{"/Pwm",pwmpage},{"/pwmset",pwmset}};

 

Resource name (URL name) can't exceed 14 char (slash included) and the query string can't exceed 64 char.

This array must by provided to getRequest function as argument.

If request is come, but the matching doesn't have success, getRequest function automatically send a "404 not found" coded response, and returns NULL.

The function activated by getRequest (callback function) must prepare and send response (see sendResponse function). Http protocol demands a mandatory response.

If the request comes with a query string (parameters list), function activated can analyze it because query string is provided as argument in this call back engine.

Parameters values can by retrieved from query string by getParameter function.

Syntax

HTTP.getRequest(int socket,int dimrs,WEBRES rs)

In version 2.2 authentication (Basic) process has been added

HTTP.getRequest(int socket,int dimrs,WEBRES rs,char *webkey)

The webkey must be coded from username and password using codeWebKey() function.

 

HTTP.codeWebKey(char user[],char psw[])

 

Parameters

socket : link socket

dimres : dimension of WEBRES array

rs : a WEBRES array

user and password for codeWebKey function: null terminated string

Returns

pointer to a char string : resource name of request or NULL

codeWebKey returns a string pointer.

 

Callback function must have this format:

Syntax

namefunction (char *query)

Parameters

pointer to a char string containing request parameters (provided by getRequest)

Returns

none , (type void)

 

To retrieve request parameters (if any) you can use getParameter function

Syntax

getParameter(char *query,int querylen, char *parametername)

Parameters

pointer to a char string (query) containing request parameters (provided by getRequest)

length of string query

name of parameter looked for

Returns

a pointer to char string contains value of parameter (as string)

or NULL if parameter is not found

Example:

#include <HTTPlib.h> //library include

 

HTTP WIFI; //instance of HTTP (inherits any stuff of MWiFi)

 

int ssocket; //server socket

int csocket=0xFF; //communication socket

 

void setup() {
    WIFI.begin(); // startup shield
:
WIFI.ConnSetOpen("DLink-casa");

int fc=WIFI.Connect();

:

ssocket=WIFI.openServerTCP(5000);


}

 

WEBRES rs[]={{"/index",pindex},{"/Analog",panalog},{"/RDigital",rdigital},
{"/WDigital",wdigital},{"/wdig",wdig},{"/Pwm",pwmpage},{"/pwmset",pwmset}};


void loop()

{

if (csocket==255) //don't poll if link already established

{

int s=WIFI.pollingAccept(ssocket);

if (s<255) csocket=s;

}

if (csocket<255) //only when link exists

{

WIFI.getRequest(csocket,7,rs);

}

}

 

void pindex(char *query) // callback function

{

makes response see sendResponse function

}

Reference Home