Sample code for 30+ languages & platforms
PureBasic

REST through HTTP Proxy

See more REST Examples

Demonstrates how to connect through an HTTP proxy to make REST API calls.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; This example connects to a REST server through an HTTP proxy.
    ; It will connect to the Amazon AWS service for this example.
    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    ; Set the HTTP proxy domain or IP address, and port.
    CkSocket::setCkHttpProxyHostname(socket, "192.168.1.100")
    CkSocket::setCkHttpProxyPort(socket, 8088)

    ; We want to tell the socket object that we're doing HTTP over the proxy connection.
    ; This is because an HTTP proxy used for other protocols (IMAP, SMTP, SSH, FTP, etc.) 
    ; can require some internal differences in behavior (i.e. how we do things).
    CkSocket::setCkHttpProxyForHttp(socket, 1)

    ; Other properties exist for specifying a proxy login, password, and authentication method (such as NTLM),
    ; but these are not used in this example.

    ; Connect through the HTTP proxy to the Amazon AWS server for the S3 service.
    bTls.i = 1
    port.i = 443
    maxWaitMs.i = 5000
    success = CkSocket::ckConnect(socket,"s3.amazonaws.com",port,bTls,maxWaitMs)
    If success <> 1
        Debug "Connect Failure Error Code: " + Str(CkSocket::ckConnectFailReason(socket))
        Debug CkSocket::ckLastErrorText(socket)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ; Use the proxied TLS connection:
    success = CkRest::ckUseConnection(rest,socket,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ; Provide AWS credentials for the REST call.
    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    CkAuthAws::setCkServiceName(authAws, "s3")
    success = CkRest::ckSetAuthAws(rest,authAws)

    ; List all buckets for the account...
    responseXml.s = CkRest::ckFullRequestNoBody(rest,"GET","/")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(socket)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

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

    success = CkXml::ckLoadXml(xml,responseXml)

    ; Show the full XML returned.
    Debug CkXml::ckGetXml(xml)

    ; Iterate over the buckets, showing each bucket name.
    success = CkXml::ckFindChild2(xml,"Buckets")
    If CkXml::ckFirstChild2(xml) = 1
        Debug CkXml::ckGetChildContent(xml,"Name")
        While (CkXml::ckNextSibling2(xml) = 1)
            Debug CkXml::ckGetChildContent(xml,"Name")
        Wend
    EndIf

    ; Move the internal pointer back to the root node.
    CkXml::ckGetRoot2(xml)


    CkRest::ckDispose(rest)
    CkSocket::ckDispose(socket)
    CkAuthAws::ckDispose(authAws)
    CkXml::ckDispose(xml)


    ProcedureReturn
EndProcedure