Sample code for 30+ languages & platforms
PureBasic

Send a REST Request with a Text Body

See more REST Examples

Demonstrates Rest.FullRequestString, a convenience method that sends a complete request whose text body (such as JSON, XML, or plain text) is supplied directly, then reads and returns the response body as text.

Background. The full-request convenience methods perform the entire request/response exchange in a single call. FullRequestString is well suited to typical JSON or XML API calls where the body fits comfortably in a string.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.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

    ;  Send a complete request whose text body is supplied directly, such as JSON.  The response body is
    ;  read as text and returned.  The 1st argument is the HTTP verb, the 2nd is the URI path, and the
    ;  3rd is the request body.
    CkRest::ckAddHeader(rest,"Content-Type","application/json")

    jsonBody.s = "{ " + Chr(34) + "name" + Chr(34) + ": " + Chr(34) + "example" + Chr(34) + ", " + Chr(34) + "active" + Chr(34) + ": true }"
    responseText.s = CkRest::ckFullRequestString(rest,"POST","/api/items",jsonBody)
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)


    ProcedureReturn
EndProcedure