Sample code for 30+ languages & platforms
PowerBuilder

Stream a REST Response Body to a Destination

See more REST Examples

Demonstrates Rest.SetResponseBodyStream, which configures a Stream destination for response bodies received by the full-request convenience methods. The body is written to the stream only when the response status code matches the expected status.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. Directing the response body to a stream avoids holding a large response in memory. The expected-status argument ensures error responses (which typically have small bodies) are not written to the stream destination.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
oleobject loo_RespStream
integer li_BAutoSetCharset
integer li_ExpectedStatus
string ls_ResponseText

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_BTls = 1
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("example.com",443,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

//  The file paths are relative to the application's current working directory.  Absolute paths
//  may also be used.  Supply the paths appropriate to your own environment.

//  Configure a Stream destination for response bodies received by the full-request convenience
//  methods.  The body is written to the stream only when the response status code matches the 1st
//  argument.
loo_RespStream = create oleobject
li_rc = loo_RespStream.ConnectToNewObject("Chilkat.Stream")

loo_RespStream.SinkFile = "qa_output/response_body.dat"

//  The 2nd argument sets the stream character set from the response Content-Type charset for textual
//  responses.  The 3rd argument is the destination Stream.
li_BAutoSetCharset = 1
li_ExpectedStatus = 200
li_Success = loo_Rest.SetResponseBodyStream(li_ExpectedStatus,li_BAutoSetCharset,loo_RespStream)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_RespStream
    return
end if

//  The full-request call now streams the response body to the sink instead of returning it.
ls_ResponseText = loo_Rest.FullRequestNoBody("GET","/api/largefile")
if loo_Rest.LastMethodSuccess = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_RespStream
    return
end if

Write-Debug "Response streamed to file.  Status: " + string(loo_Rest.ResponseStatusCode)


destroy loo_Rest
destroy loo_RespStream