Skip to main content

Accessing the Data of the Response

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()
FeedbackOpens in a new tab