Sample code for 30+ languages & platforms
Xbase++

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

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. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nBAutoReconnect
LOCAL oBdRequest
LOCAL nBLoaded
LOCAL oSbResponse
LOCAL cResponseText

nSuccess := 0

oRest := CreateObject("Chilkat.Rest")
nBTls := 1
nBAutoReconnect := 1
nSuccess := oRest:Connect("example.com", 443, nBTls, nBAutoReconnect)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

//  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.

//  FullRequestBd sends a request whose binary body is read from a BinData and stores the text
//  response body in a StringBuilder.
oBdRequest := CreateObject("Chilkat.BinData")
nBLoaded := oBdRequest:LoadFile("qa_data/image.png")
IF (nBLoaded != 1)
    ? "Failed to load the request body file."
    oRest:destroy()
    oBdRequest:destroy()
    RETURN
ENDIF

oRest:AddHeader("Content-Type", "image/png")

oSbResponse := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestBd("PUT", "/api/images/1", oBdRequest, oSbResponse)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    oBdRequest:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

cResponseText := oSbResponse:GetAsString()
IF (oSbResponse:LastMethodSuccess == 0)
    ? oSbResponse:LastErrorText
    oRest:destroy()
    oBdRequest:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

? cResponseText

oRest:destroy()
oBdRequest:destroy()
oSbResponse:destroy()