Question
· Jul 27, 2021

Consume Third party API from Iris

Hi Team,

I want to consume(call) third-party API by passing JSON as a request from Iris.

Product version: IRIS 2020.1
Discussion (5)0
Log in or sign up to continue

I'm about to go down the same path here. I have a rough idea of what I need to do. I'm going to try to use the %Net.HttpRequest object and do at least the following steps:

1. Create a new %Net.HttpRequest object

set myrequest = ##class(%Net.HttpRequest).%New()

2. Set the server

set myrequest.server = "www.whatever.com"

3. Set the locatoin

set myrequest.location = "/path/to/rest"

4. Create a global binary stream.

5. Write json data to the stream.

6. Use the stream as the EntityBody for the HttpRequest.

7. Call the get, put, or post method of the HttpRequest object to consume.

8. Use the HttpRequest's HttpResponse object to check the response

I've had some time to try this now. Here are steps that worked for me:

set myrequest = ##class(%Net.HttpRequest).%New()
set myrequest.Server = "<server ip or domain here>"
set myrequest.Port = "<server port here if it isn't 80>"
set myrequest.Location = "</path/to/rest>"
do myrequest.EntityBody.Write("<your json here>")
do myrequest.Post()
set mydata = myrequest.HttpResponse.Data.Read()

At that point, the data returned in the response should be in mydata.

Depending on your specific API, you made need to take additional steps for authentication, and you may need to use myrequest.Get() or myrequest.Put() instead of myrequest.Post().

If you need to set parameters, you use the SetParam method of the HttpRequest. For example, if you're using the very most basic way to authenticate to a Cache instance, you do that by specifying a CacheUserName and a CachePassword as parameters as follows any time before your post/put/get:

do myrequest.SetParam("CacheUserName","<your username here>")
do myrequest.SetParam("CachePassword","<your password here>")