Question
· May 16, 2022

Send HTTP request and get a response

Hi Guys,

 

is there a sample code where I can  basically send a HTTP request given a URL and authentication and handle the JSON response?

 

Thanks

Product version: Ensemble 2014.1
Discussion (10)2
Log in or sign up to continue

Hi.

Set request=##class(%Net.HttpRequest).%New()
Set request.Server="something.com"
Set request.Port="some_port"
Set request.Username="some_username"
Set request.Password="some_password"

Set status=request.Get("some_url")
Do request.HttpResponse.Data.Rewind()
Set jsonString=request.HttpResponse.Data.Read(request.HttpResponse.Data.Size,.sc)

Set jsonObject={}.%FromJSON(jsonString)
Write jsonObject."some_property"
...

Something like that...

Regards,
Matjaž

Sure, here's one:

set req = ##class(%Net.HttpRequest).%New()
set req.SSLConfiguration="MBTA" // Set this up in the management portal under Security Management > SSL/TSL Configurations; this is the SSLConfig's "Name"
// For HTTP Basic authentication, simple route is e.g.:
set req.Username = "foo"
set req.Password = "bar"
// For other modes, e.g.:
do req.SetHeader("Authorization","Bearer abcd")
set sc = req.Get("https://api-v3.mbta.com/routes")
// TODO: check sc / $$$ThrowOnError(sc)
// TODO: check req.HttpResponse.StatusCode to make sure it's 200 and error handle appropriately
set obj = {}.%FromJSON(req.HttpResponse.Data)
zw obj // There's your object!

mybe you mean this  ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(SampleJsonData,,.list) ,I tried that but I got an empty list?

in all cases, after doing the below, I should be able at lease read the JSON string but I'm getting an html file, so I'm missing something in my code?  

set Httprequest = ##class(%Net.HttpRequest).%New()                   
Set Httprequest.SSLConfiguration="RTLS"                              
Set Httprequest.Server="serverurl"                 
Set Httprequest.Https=1                                              
Set Httprequest.Timeout=30                                           
set Httprequest.ContentType="application/json"                       
Do Httprequest.SetHeader("Accept","text/plain") set Httprequest.Username="user"                      
set Httprequest.Password="password"                              
S stat= Httprequest.Get("someurl")

Set Resp=Httprequest.HttpResponse.Data.Read()

Status= 1 justteslls you tht the HTTP connection was processed OK.
Next you need to take a look into your response object.

set res=Httprequest.HttpResponse
in terminal then ZW res to see in the Status is 200 OK
otherwise what you see I  res.Data might be just an error page.
Which is also OK for HTTP but not for your content.

• property ReasonPhrase as %String;

This is the human readable reason that goes with the StatusCode.

• property StatusCode as %Integer;

The HTTP status code. This is useful to determine if the request was successful. Look in the rfc for HTTP to see which codes are supported and what they mean. A human readable form of this code is stored as the ReasonPhrase

• property StatusLine as %String;

The HTTP status line. This is the first line of the response and signals if the request was successful or if there was a problem.

So I did the following from the Terminal:

set Httprequest = ##class(%Net.HttpRequest).%New()                   
Set Httprequest.SSLConfiguration="RTLS"                              
Set Httprequest.Server="serverurl"                 
Set Httprequest.Https=1                                              
Set Httprequest.Timeout=30                                           
set Httprequest.ContentType="application/json"                       
Do Httprequest.SetHeader("Accept","text/plain")
 
set Httprequest.Username="user"                      
set Httprequest.Password="password"                              
S stat= Httprequest.Get("someurl")

W stat
1

so given the status =1 that means everything went ok isn't if not how can I check?

then I did the following to read the JSON file : 
Set Resp=Httprequest.HttpResponse.Data.Read()

but when I cheked what's in Resp I get the below html output:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>IUDEX</title>
    <base href="/" />
 
    <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
    <link href="_content/Smart.Blazor/css/smart.default.css" rel="stylesheet" />
 
    <link href="css/app.css" rel="stylesheet" />
 
    <link href="Galaxy.Client.styles.css" rel="stylesheet" />
</head>
<body>
    <style>
        .loader-logo {
            width: 256px;
        }
 
        .loader-container {
            flex-direction: column;
            flex-wrap: nowrap;
            justify-content: center;
            align-items: center;
            display: flex;
            gap: 32px;
            height: 512px;
        }
 
        .spinner {
            border: 5px solid #afafaf;
            border-top: 5px solid #e53947;
            border-radius: 50%;
            width: 32px;
            height: 32px;
            animation: spin 700ms linear infinite;
        }
 
        @keyframes spin {
            0% {
                transform: rotate(0deg)
            }
 
            100% {
                transform: rotate(360deg)
            }
        }
    </style>
 
    <div class="loader-container" id="app">
        <img class="loader-logo" src="images/logo-dark.svg" />
        <div class="spinner"></div>
        <div id="blazor-error-ui">
            An unhandled error has occurred.
            <a href="" class="reload">Reload</a>
            <a class="dismiss">🗙</a>
        </div>
    </div>
 
 
 
    <script src="_content/MudBlazor/MudBlazor.min.js"></script>
 
    <script src="_content/Smart.Blazor/js/smart.blazor.js"></script>
    <script src="_content/Smart.Blazor/js/smart.elements.js"></script>
    <script src="_content/Galaxy.DataViewer/canvasjs.min.js"></script>
 
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="js/galaxyLib.min.js"></script>
 
</body>
</html>

I thought I'll be getting a JSON string, FYI I'm supposed to a get a Token in a JSON file?

Thanks