DataFlex
DataFlex
Send a Staged REST Request with a Binary Body
See more REST Examples
Demonstrates Rest.SendReqBd, which sends a request whose binary body is read from a BinData, then reads the response in separate steps.
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Boolean iBAutoReconnect
Variant vBdBody
Handle hoBdBody
Boolean iBLoaded
Integer iStatusCode
String sResponseBody
String sTemp1
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.
// Send a request whose binary body is read from a BinData.
Get Create (RefClass(cComChilkatBinData)) To hoBdBody
If (Not(IsComObjectCreated(hoBdBody))) Begin
Send CreateComObject of hoBdBody
End
Get ComLoadFile Of hoBdBody "qa_data/image.png" To iBLoaded
If (iBLoaded <> True) Begin
Showln "Failed to load the request body file."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "image/png" To iSuccess
Get pvComObject of hoBdBody to vBdBody
Get ComSendReqBd Of hoRest "PUT" "/api/images/1" vBdBody To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// After sending, read the response status line and header fields. The return value is the HTTP
// status code, or -1 if no response was received.
Get ComReadResponseHeader Of hoRest To iStatusCode
If (iStatusCode < 0) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Response status code: " iStatusCode
// Read the response body as text.
Get ComReadRespBodyString Of hoRest To sResponseBody
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sResponseBody
End_Procedure