Sample code for 30+ languages & platforms
PureBasic

REST through SOCKS Proxy

See more REST Examples

Demonstrates how to connect through a SOCKS 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 a SOCKS 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 SOCKS proxy domain or IP address, port, and SOCKS version number (4 or 5)
    CkSocket::setCkSocksHostname(socket, "192.168.1.100")
    CkSocket::setCkSocksPort(socket, 1080)
    CkSocket::setCkSocksVersion(socket, 5)
    ; Other properties exist for specifying a SOCKS proxy login and password,
    ; 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