DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Boolean iBAutoReconnect
Variant vRespStream
Handle hoRespStream
Boolean iBAutoSetCharset
Integer iExpectedStatus
String sResponseText
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Move True To iBTls
Move True To iBAutoReconnect
Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatStream)) To hoRespStream
If (Not(IsComObjectCreated(hoRespStream))) Begin
Send CreateComObject of hoRespStream
End
Set ComSinkFile Of hoRespStream To "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.
Move True To iBAutoSetCharset
Move 200 To iExpectedStatus
Get pvComObject of hoRespStream to vRespStream
Get ComSetResponseBodyStream Of hoRest iExpectedStatus iBAutoSetCharset vRespStream To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// The full-request call now streams the response body to the sink instead of returning it.
Get ComFullRequestNoBody Of hoRest "GET" "/api/largefile" To sResponseText
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "Response streamed to file. Status: " iTemp1
End_Procedure