Learning
Documentation
Community
Open Exchange
Global Masters
Home / Class Reference / %SYS namespace / %Net.Http
Private  Storage   

%Net.Http


abstract class %Net.Http

This class implements various utility functions for HTTP.

Inventory


Parameters Properties Methods Queries Indices ForeignKeys Triggers
1


Summary


Methods
getJSON


Methods


• classmethod getJSON(requestURL As %RawString = "", request As %String(MAXLEN="")="") as %DynamicAbstractObject [ SQLProc = getJSON ]

getJSON() is a utility function that accepts a URL and an optional request object. If a request object is supplied then it is expected to be either JSON or an oref. The properties of the request object will be used to populate the HttpRequest before submitting the request. The response is expected to be encoded as JSON.

The value returned by getJSON() is an instance of %Library.DynamicAbstractObject. If an error is encountered during execution then an exception is thrown.

set continents = ##class(%Net.Http).getJSON("http://localhost:57772/api/document/v1/SAMPLES/continents",{"Username":"_SYSTEM","Password":"SYS"})

The requestURL is a string containing the URL to which the request is sent. If this argument is incomplete or not specified then the URL property from the request object can be used to as the target URL. Other properties of the request object can be used to complete the URL. For example, the Port property in this example:

USER>try { set continents = ##class(%Net.Http).getJSON("http://localhost/api/document/v1/SAMPLES/continents",{"Username":"_SYSTEM","Password":"SYS","Port":57772}) write continents.%ToJSON() } catch (exc) { do $system.OBJ.DisplayError(exc.AsStatus()) }
{"collection":"continents","size":8,"content":[
{"documentID":1,"documentVersion":1,"content":{"code":"NA","name":"North America"}},
{"documentID":2,"documentVersion":2,"content":{"code":"SA","name":"South America"}},
{"documentID":3,"documentVersion":3,"content":{"code":"AF","name":"Africa"}},
{"documentID":4,"documentVersion":4,"content":{"code":"AS","name":"Asia"}},
{"documentID":5,"documentVersion":5,"content":{"code":"EU","name":"Europe"}},
{"documentID":6,"documentVersion":6,"content":{"code":"OC","name":"Oceana"}},
{"documentID":7,"documentVersion":7,"content":{"code":"AN","name":"Antarctica"}},
{"documentID":9,"documentVersion":8,"content":{}}
]}

If request is passed then it must either be valid JSON or an oref of type %Library.DynamicAbstractObject. The properties contained in request must be named the same as a property in %Net.HttpRequest. The exception is the url property. Any other property contained in request that is not named the same as a property in %Net.HttpRequest will trigger an exception:

USER>try { set continents = ##class(%Net.Http).getJSON("http://localhost/api/document/v1/SAMPLES/continents",{"Username":"_SYSTEM","Password":"SYS","port":57772}) } catch (exc) { do $system.OBJ.DisplayError(exc.AsStatus()) }

ERROR #5035: General exception Name 'Compose' Code 'Compose' Data 'Strict mode and source contains unmapped property, %Library.DymamicObject instance to %Net.HttpRequest'

If requestURL is not passed or is passed as null then the request.url property is used as the URL.

This method is projected as an SQL function and can be invoked in SQL as %Net.getJSON().

USER>d $system.SQL.Shell()
SQL Command Line Shell
----------------------------------------------------

The command prefix is currently set to: <>.
Enter q to quit, ? for help.
USER>>select code, name from JSON_TABLE(%Net.getJSON('http://localhost/api/document/v1/SAMPLES/continents','{"Username":"_SYSTEM","Password":"SYS","Port":57772}'),'$.content' columns (document varchar(2000) path '$',code varchar(2) path '$.content.code',name varchar(50) path '$.content.name')) order by name
4.	select code, name from JSON_TABLE(%Net.getJSON('http://localhost/api/document/v1/SAMPLES/continents','{"Username":"_SYSTEM","Password":"SYS","Port":57772}'),'$.content' columns (document varchar(2000) path '$',code varchar(2) path '$.content.code',name varchar(50) path '$.content.name')) order by name

code	name
	
AF	Africa
AN	Antarctica
AS	Asia
EU	Europe
NA	North America
OC	Oceana
SA	South America

8 Rows(s) Affected