restReqUrlGet (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

ALL

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 devicesGetCallback: INT;
VAR_INPUT
  req         : SYSHANDLE; // Request
  resp        : SYSHANDLE; // Response
  arg         : DINT;     // User argument
END_VAR;
VAR
  str, name, url:STRING;
  body  : STRING := "";
  rc    : INT;
END_VAR;
  // Show client address
  restReqClientAddressGet (req:=req, address:=str);
  DebugFmt(message:="Request type \4 from "+str, v4:=arg);
 
  // Read body content from previous callback, if it is present
  restRespBodyGetString(resp:=resp, str:=body);
 
  // Add header for this callback to body
  body := body+" -- EP "+dintToStr(v:=arg)+" -- $N";
 
  // Get request URL
  rc := restReqUrlGet(req:=req, method:=str, url:=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:=resp, str := body);
  // Set content-type
  restRespHeaderSet(resp:=resp, name:="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;