Sample code for 30+ languages & platforms
PureBasic

Send a REST Request with a Streamed Body

See more REST Examples

Demonstrates Rest.FullRequestStream, which sends a complete request whose body is read from a Stream, then returns the response body as text.

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. Streaming the body is appropriate for large uploads because the data is read incrementally from the source (here a file) rather than being held entirely in memory.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkStream.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    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.

    ;  The request body is read from a Stream.  Here the stream's source is a local file, which is
    ;  appropriate for large uploads because the data need not be held entirely in memory.
    bodyStream.i = CkStream::ckCreate()
    If bodyStream.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSourceFile(bodyStream, "qa_data/upload.json")

    CkRest::ckAddHeader(rest,"Content-Type","application/json")

    ;  The 3rd argument is the Stream that supplies the request body.
    responseText.s = CkRest::ckFullRequestStream(rest,"POST","/api/upload",bodyStream)
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStream::ckDispose(bodyStream)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)
    CkStream::ckDispose(bodyStream)


    ProcedureReturn
EndProcedure