Written by

Senior Software Engineer
Question Ashok Kumar T · Dec 11, 2024

HTTP Message format from %Net.HttpRequest

Hello Community,

Is there any built in methods available to generate the both HTTP message format(request/response) from %Net.HttpRequest and it's response. 

Thanks!

Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1.1 (Build 347U)

Comments

Enrico Parisi · Dec 11, 2024

I do not understand your question, can you please explain what you need to achieve?

%Net.HttpRequest class is used to create an HTTP request that is sent to the counterpart/endpoint and a response message is then received from the counterpart. How can you possibly generate a response that, by definition, has to be received from a remote system?

Clearly I'm missing something.

0
Ashok Kumar T  Dec 11, 2024 to Enrico Parisi

Hello @Enrico Parisi 

I'm expect/built in method to generate the below HTTP request and HTTP response from %Net.HttpRequest. which was shown in the "View HTTP Trace" option in web gateway.

POST /aktest/test HTTP/1.1
Content-Type: application/fhir+json
Accept: */*
Host: localhost:52773
Accept-Encoding: gzip, deflate, br
Content-Length: 21

{ "asd":"asd" }
0
Enrico Parisi  Dec 11, 2024 to Ashok Kumar T

Code adapted with your details:

Set objHttpRequest = ##class(%Net.HttpRequest).%New()
Set objHttpRequest.ContentType = "application/fhir+json"Set objHttpRequest.Server = "localhost"Set objHttpRequest.Port = "52773"Set pRequest = {"asd":"asd"}
Do objHttpRequest.EntityBody.Write(pRequest.%ToJSON())
If objHttpRequest.Send("POST", "/aktest/test") {
    Set objHttpResponse = objHttpRequest.HttpResponse
    If$IsObject(objHttpResponse.Data) {
        Set objStream = objHttpResponse.Data
        Set json = ""While ('objStream.AtEnd) {
            Set json = json _ objStream.ReadLine()
        }
    } Else {
        Set json = objHttpResponse.Data
    }
    Set httpStatus = objHttpResponse.StatusCode
    Write"Status: ", httpStatus, !
    Write"Response: ", json, !
}
0
Ashok Kumar T  Dec 11, 2024 to Enrico Parisi

Thanks for the code samples. my expectation is to convert the %Net.HttpRequest to Http request standard message structure. Actually Mr @Jeffrey Drumm gives me the expected solution. The Http Get , Post methods has the second parameter "test" it helps me to get that standard http message string

0
Jeffrey Drumm · Dec 11, 2024

Are you looking to view the expected request and/or response? The %Net.HttpRequest Send() method has a test argument as its 3rd option; setting it to 1 outputs the request, 2 the response, and 3 the response headers.

It's unfortunate that this isn't described in the class documentation; you have to look at the source to figure out what the test argument does.

0
Ashok Kumar T  Dec 11, 2024 to Jeffrey Drumm

Thanks for pointing the solution. By using this integer we can get the request if test is 1or response test=2 or test = 3 headers. However, As of my understanding we didn't get both request and response at the same time.

0
Eduard Lebedyuk  Dec 11, 2024 to Ashok Kumar T

1 outputs but does not send the request, so just call Send with 1 first, then with 2 to get both request and response.

0