Sample code for 30+ languages & platforms
PowerBuilder

Send a Staged REST Request with a Streamed Body

See more REST Examples

Demonstrates Rest.SendReqStreamBody, which sends a request whose body is read from a Stream, 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
oleobject loo_BodyStream
integer li_StatusCode
string ls_ResponseBody

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.

//  The request body is read from a Stream whose source is a local file.
loo_BodyStream = create oleobject
li_rc = loo_BodyStream.ConnectToNewObject("Chilkat.Stream")

loo_BodyStream.SourceFile = "qa_data/upload.json"

loo_Rest.AddHeader("Content-Type","application/json")

//  Send the request, reading the body from the stream.  The 3rd argument is the Stream.
li_Success = loo_Rest.SendReqStreamBody("POST","/api/upload",loo_BodyStream)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_BodyStream
    return
end if

//  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.
li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_BodyStream
    return
end if

Write-Debug "Response status code: " + string(li_StatusCode)

//  Read the response body as text.
ls_ResponseBody = loo_Rest.ReadRespBodyString()
if loo_Rest.LastMethodSuccess = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_BodyStream
    return
end if

Write-Debug ls_ResponseBody


destroy loo_Rest
destroy loo_BodyStream