restReqUrlGet (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

2.10.00


This function will read the URL and method of a request.

For outgoing requests, this is the URL and method specified in restReqCreate.

For incoming requests, this is the relative URL and method used by the client, including any query parameters.

 

Input:

req : SYSHANDLE

A handle to the request.

 

Output:

method : STRING

The HTTP method of the request, e.g. "GET" or  "POST".

 

url : STRING

The URL of the request.

 

 

Returns: INT

1


- Success

0


- Not supported

-1


- Invalid request

-2


- Request does not contain an URL

 

Declaration:

FUNCTION restReqUrlGet : INT;
VAR_INPUT
   req            : SYSHANDLE;
   method         : ACCESS STRING;
   url            : ACCESS STRING;
END_VAR;

 

 

Example:

 
 
FUNCTION CALLBACK devicesGetCallbackINT;
VAR_INPUT
   req         : SYSHANDLE// Request
   resp        : SYSHANDLE// Response
   arg         : DINT;      // User argument
END_VAR;
VAR
   strnameurl:STRING;
   body  : STRING := "";
   rc    : INT;
END_VAR;
   // Show client address
   restReqClientAddressGet (req:=reqaddress:=str);
   DebugFmt(message:="Request type \4 from "+strv4:=arg);
   
   // Read body content from previous callback, if it is present
   restRespBodyGetString(resp:=respstr:=body);
   
   // Add header for this callback to body
   body := body+" -- EP "+dintToStr(v:=arg)+" -- $N";
   
   // Get request URL
   rc := restReqUrlGet(req:=reqmethod:=strurl:=url);
   IF rc 0 THEN
      DebugFmt(message:=" "+str+" "+url);
      body:=body+str+" "+url+"$N";
   ELSE
      DebugFmt(message:="restReqUrlGet: \1", v1:=rc);
   END_IF;
   
   // Set response body
   restRespBodySetString(resp:=respstr := body);
   // Set content-type
   restRespHeaderSet(resp:=respname:="Content-Type", value:="text/plain");
   
   // Return 0 to continue with the next callback. 
   // Return status code to stop and return the response.
   devicesGetCallback := 0;
   
END_FUNCTION;