PowerBuilder
PowerBuilder
Read the REST Response Body into a Stream
See more REST Examples
Demonstrates Rest.ReadRespBodyStream, which reads the response body into a Stream. The optional flag sets the stream character set from the response Content-Type charset for textual responses.
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. The staged request model separates sending from reading: a
SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
integer li_StatusCode
oleobject loo_SbError
oleobject loo_RespStream
integer li_BAutoSetCharset
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.
li_Success = loo_Rest.SendReqNoBody("GET","/api/images/1")
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
// Check for the expected success status code. If the response is not 200, the body is likely
// error text formatted as JSON, XML, or HTML rather than the expected binary content. In that case
// read the body as text, report it, and return.
if li_StatusCode <> 200 then
loo_SbError = create oleobject
li_rc = loo_SbError.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Rest.ReadRespSb(loo_SbError)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
destroy loo_SbError
return
end if
Write-Debug "Request failed with status code " + string(li_StatusCode)
Write-Debug loo_SbError.GetAsString()
destroy loo_Rest
destroy loo_SbError
return
end if
// The status code is 200. Read the response body into a Stream. Here the stream's sink is a local
// file, which is appropriate for large or binary responses.
loo_RespStream = create oleobject
li_rc = loo_RespStream.ConnectToNewObject("Chilkat.Stream")
loo_RespStream.SinkFile = "qa_output/image.png"
// If the 2nd argument is 1 and the response is textual, the stream's character set is set from
// the response Content-Type charset. It has no effect for binary responses.
li_BAutoSetCharset = 1
li_Success = loo_Rest.ReadRespBodyStream(loo_RespStream,li_BAutoSetCharset)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
destroy loo_SbError
destroy loo_RespStream
return
end if
Write-Debug "Response body written to the stream sink."
destroy loo_Rest
destroy loo_SbError
destroy loo_RespStream