Web Connection
Returning Json cursor from REST function
Gravatar is a globally recognized avatar based on your email address. Returning Json cursor from REST function
  Luca
  All
  Nov 23, 2021 @ 02:20am

Dear Rick,
I am trying to return a Json cursor from a Rest function: your example runs rightly, but how to close the cursor before the end of procedure?
In this way I leave one cursor open every time.
Thank you for support

FUNCTION Customers()

SELECT * FROM Customers ;
      ORDER BY Company ;
      INTO CURSOR TCustomers

*** Override any property names with special case
JsonSerializer.PropertyNameOverrides = "firstName,lastName" 

RETURN "cursor:TCustomers"
Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Tore Bleken
  Luca
  Nov 23, 2021 @ 02:53am

Simply add one line wherever you want:

Use in TCustomer
Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Luca
  Tore Bleken
  Nov 23, 2021 @ 04:37am

No Tore,
if I close the cursor the function I get "error reading JToken from JsonReader" because the function cannot find the cursor to return.

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Tore Bleken
  Luca
  Nov 23, 2021 @ 04:49am

Can't you just close it at a later stage? Or zap it?

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Luca
  Tore Bleken
  Nov 23, 2021 @ 05:28am

I develop a library of Rest services, everyone is called individually with its own parameters (for example, RestService1?Id=45&Time=Month).
I would like to close every temporary resource used by Rest functions before returning values, but I cannot close the cursor passed to Json.
Please is there any way to store Json return value to some variable before RETURN?

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Tore Bleken
  Luca
  Nov 23, 2021 @ 05:30am

Json is just text, so you can do anything you like with it.

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Luca
  Tore Bleken
  Nov 23, 2021 @ 05:55am

Oh, fantastic!
So how I capture

RETURN "cursor:TCustomers"

into a variable before closing the function?

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Tore Bleken
  Luca
  Nov 23, 2021 @ 06:20am

I'm sorry, I really don't understand your problem. Hopefully Rick does, and can give you the help you want.

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Rick Strahl
  Luca
  Nov 23, 2021 @ 02:02pm

You can't close the cursor before the end of the routine because it's needed to render the JSON result.

There's an OnProcessComplete() method that you can override. In that method you can check for open cursors or whatever and close them.

FUNCTION OnProcessComplete()

IF USED("TQuery")
   USE IN TQuery
ENDIF

Note this fires for every request in your process class, so if the code is specific to your method you might have to filter by the script name. For Rest Services you can also use OnAfterCallComplete(lcResult) which receives the final output of the request. This is probably the better way to go for a REST service.

ENDFUNC

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Returning Json cursor from REST function
  Luca
  Rick Strahl
  Nov 24, 2021 @ 02:30am

Thank you Rick!
I love Web Connection.

© 1996-2021