Sample code for 30+ languages & platforms
PowerBuilder

Read the REST Response Body into BinData

See more REST Examples

Demonstrates Rest.ReadRespBd, which reads the response body as raw bytes into a BinData, replacing its previous contents.

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
integer li_StatusCode
oleobject loo_SbError
oleobject loo_BdResponse
integer li_BSaved

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 as raw bytes into a BinData.
loo_BdResponse = create oleobject
li_rc = loo_BdResponse.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Rest.ReadRespBd(loo_BdResponse)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_SbError
    destroy loo_BdResponse
    return
end if

li_BSaved = loo_BdResponse.WriteFile("qa_output/image.png")
if li_BSaved <> 1 then
    Write-Debug "Failed to save the response body."
    destroy loo_Rest
    destroy loo_SbError
    destroy loo_BdResponse
    return
end if

Write-Debug "Received " + string(loo_BdResponse.NumBytes) + " bytes."


destroy loo_Rest
destroy loo_SbError
destroy loo_BdResponse