Question
· Jun 27

Http Request and Response format from %request object

Hello community,
Is there any built in class method to get the HTTP request and response message format from the %request and %response object. Similar to how a Web Gateway's HTTP Trace Facility captures HTTP messages.

Thanks!

Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1.1 (Build 347U) Thu Jul 18 2024 17:40:10 EDT
Discussion (7)4
Log in or sign up to continue

A built-in method to generate HTTP request and response messages from the %request and %response objects is not explicitly available as described in common documentation. However, if your question is about the GET or POST request handling using %Net.HttpRequest class, you can use the test argument of the Send method of %Net.HttpRequest to view the HTTP message formats:

  1. test=1: Outputs the HTTP request message without sending it.
  2. test=2: Outputs the HTTP response message.
  3. test=3: Outputs only the response headers.

For example:

Set objHttpRequest = ##class(%Net.HttpRequest).%New()
Set objHttpRequest.Server = "api.restful-api.dev"
Set objHttpRequest.Port = "443"
Set objHttpRequest.Https = 1
Set objHttpRequest.SSLConfiguration = "pm.community.intersystems.com"
Set pRequest = {
   "name": "Apple MacBook Pro 16",
   "data": {
      "year": 2019,
      "price": 1849.99,
      "CPU model": "Intel Core i9",
      "Hard disk size": "1 TB"
   }
}
Do objHttpRequest.EntityBody.Write(pRequest.%ToJSON())
Set objHttpRequest.ContentType = "application/json"

// Generate the HTTP request format
Do objHttpRequest.Send("POST", "/objects", 1)

// Send request and fetch HTTP response format
If objHttpRequest.Send("POST", "/objects", 2) { 
    Set objHttpResponse = objHttpRequest.HttpResponse 
}

Note: I used a public REST API just for test purpose.

Using test=1, you can view the raw request; test=2 lets you observe the returned response while also executing the request [1].

Fontes:

Do you mean the format as in HL7, FHIR, etc.? There are specific ContentType settings for those in the specification, I believe.

  • If the message is a "vertical bar" encoded HL7 v2.x message, the content type SHALL be:     x-application/hl7-v2+er7
  • If the message is an XML encoded HL7 v2.x message, the content type SHALL be:     x-application/hl7-v2+xml
  • If the message is an XML encoded HL7 v3 message, the content type SHALL be:     x-application/hl7-v3+xml
  • If the message is an XML encoded FHIR message, the content type SHALL be:     x-application/fhir+xml
  • If the message is a JSON encoded FHIR message, the content type SHALL be:     x-application/fhir+json
  • If the message is a CDA document, the content type SHALL be:     x-application/xml+cda

Please provide more context and details when you post questions.

To get the request submitted body (if any) you can use the Content property of the %CSP.Request object (i.e. %request).

For the response it's not possible, the response is "sent to the browser" (via Web Gateway....) immediately, there is no buffer or anything.

If you want to capture YOUR response, put it in a buffer and when you are done, send it from your buffer "to the browser"

Hello @Enrico Parisi 

I'm working on capturing and logging all REST request and response data for application endpoints. Currently, I'm able to extract the incoming request using the %request object and handle it within the OnPreDispatch method. My goal is to serialize this request into raw HTTP format for storage and analysis. So, I'm if any method available to create the HTTP format,If no built-in method exists to generate the raw HTTP request format, I plan to implement one manually.

The challenge arises from the manually created REST services are implemented: each REST call directly maps to a class method invocation <Route Url="/test" Method="POST" Call="User.Sample:Test" Cors="true"/> , As a result, capturing the response data would require modifying multiple class methods to intercept output, which is not feasible.

To avoid this, I'm looking for a way to extract the full HTTP response—ideally from a buffer it's been written and accessible, I specifically want to avoid enabling HTTP tracing on the Web Gateway due to performance and logging concerns.

Thanks!

REST code write the response to the current device.

An idea (to be tested) could be to intercept the calls in early stages, save the current device and change the current device to "some other device" (to be defined), then, after the code has finished, save the output to your log and switch to the "old" current device and write the output.

I think it can be done, it surely have some performance impact but...maybe feasible.

Yes, there is such method.

%CSP.Request has method "GetCgiEnv" that you can use like this:

ClassMethod YourMethod(request As %CSP.Request) As %Status

{

        Set userAgent = request.GetCgiEnv("HTTP_USER_AGENT")

        Set IPAddress = request.GetCgiEnv("REMOTE_ADDR")

        Set messageFormat= request.GetCgiEnv("CONTENT_TYPE")

       return $$$OK

}

All possible data that you can get from GetCgiEnv you can look here: Cgi Data