Consuming a REST Service
PeopleCode, RESTThe code for calling a REST service is thankfully very simple. There is a much, much longer way to call a REST service where you create Documents, sub Documents, Messages, Service Operations and on and on and on, but this example uses the .ConnectorRequest method found in the %IntBroker
Class. There is an example and a little more information here in PeopleBooks.
Local Message &Request, &Response;
Local boolean &b;
/* createRequest */
&Request = CreateMessage(Message.IB_GENERIC);
&Request.IBInfo.IBConnectorInfo.ConnectorName = "HTTPTARGET";
&Request.IBInfo.IBConnectorInfo.ConnectorClassName = "HttpTargetConnector";
/* setHTTPProperties */
&b = &Request.IBInfo.IBConnectorInfo.AddConnectorProperties("Method", "GET", %HttpProperty);
&b = &Request.IBInfo.IBConnectorInfo.AddConnectorProperties("URL", "https://api.example.com/api", %HttpProperty);
/* setHeaders */
&b = &Request.IBInfo.IBConnectorInfo.AddConnectorProperties("accept", "application/json", %Header);
&b = &Request.IBInfo.IBConnectorInfo.AddConnectorProperties("authorization", &yourTokenString, %Header);
&b = &Request.IBInfo.IBConnectorInfo.AddConnectorProperties("another-header", &anotherValue, %Header);
/* getResponse */
local string &json;
&Response = %IntBroker.ConnectorRequest(&Request, True);
&json = &Response.GetContentString()
There is a post here that covers reading the resulting json into an array.
This should work out-of-the-box, but sometimes servers are not configured correctly. If they aren’t configured correctly you’ll get nothing back from .GetContentString
. You will find the errors in a server’s log file, somewhere.
Here is an Environment Checklist
- Service Operation IB_GENERIC is active.
- Service Operation IB_GENERIC’s only routing is active.
- PeopleTools > Web Profile > Web Profile Config >
> Authorized Sites > Add target URIs - Any and all firewalls are configured.
- Ports (443 for example) are open.