Skip to main content

Using an HTTP Response

This page describes how to use an HTTP response object (an instance of %Net.HttpResponseOpens in a new tab).

Accessing the Data of the Response

The body of the HTTP response is contained in the Data property of the response. This property contains a stream object, specifically %GlobalBinaryStreamOpens in a new tab. Note that %GlobalBinaryStreamOpens in a new tab is deprecated but is still supported for use in this way. It is not recommended to substitute a different stream class for this use case.

To work with this stream, use the stream methods Write(), WriteLine(), Read(), ReadLine(), Rewind(), MoveToEnd(), and Clear(). You can also use the Size property of the stream.

The ReadRawMode property of the request controls how the body of the response is read.

  • By default, this property is false and InterSystems IRIS assumes that the body is in the character set specified in the HTTP headers of the response (and translates the character set accordingly).

  • If this property is true, InterSystems IRIS reads the body in RAW mode (performing no translation of the character set).

You can also use the OutputToDevice() method, which writes the full response to the current device. The headers are not in the same order as generated by the web server.

The following shows a simple example in which we copy the response stream to a file and save it:

 set request=##class(%Net.HttpRequest).%New()
 set request.Server="tools.ietf.org"
 set request.Https=1
 set request.SSLConfiguration="TEST"
 set status=request.Get("/html/rfc7158")
 if $$$ISERR(status) {
         do $system.OBJ.DisplayError()
 } else {
         set response=request.HttpResponse
 }
 
 Set file=##class(%FileCharacterStream).%New()
 set file.Filename="c:/temp/rfc7158.html"
 set status=file.CopyFrom(response.Data)
 if $$$ISERR(status) {
         do $system.OBJ.DisplayError()
 }
 do file.%Close()

Getting an HTTP Header by Name

The %Net.HttpResponseOpens in a new tab class stores its HTTP headers in an InterSystems IRIS multidimensional array. To access the headers, use the following methods:

GetHeader()

Returns the value of the given header.

GetNextHeader()

Returns the name of the next header after the given header.

Each of these methods takes a single argument, a string that is the name of an HTTP header.

You can also use the OutputHeaders() method, which writes the HTTP headers to the current device (although not in the same order they were generated).

Accessing Other Information about the Response

The %Net.HttpResponseOpens in a new tab class provides properties that store other specific parts of the HTTP response:

  • StatusLine stores the HTTP status line, which is the first line of the response.

  • StatusCode stores the HTTP status code.

  • ReasonPhrase stores the human-readable reason that corresponds to StatusCode.

  • ContentInfo stores additional information about the response body.

  • ContentType stores the value of the Content-Type: header.

  • HttpVersion indicates the version of HTTP that is supported by the web server that sent the response.

See Also

FeedbackOpens in a new tab