Sample code for 30+ languages & platforms
PureBasic

REST URL Encode Path Parts and Query Params

See more REST Examples

When passing a path to a Chilkat REST function, the path parts and query params should be URL encoded. This example explains..

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkRest.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 demonstrates how to URL encode the path passed to a REST function.
    ; It is demonstrated with an Amazon SP API GET request to get details about a listings item for a selling partner.
    ; See https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#getlistingsitem

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

    ; Connect to the REST server.
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"sellingpartnerapi-eu.amazon.com",port,bTls,bAutoReconnect)

    CkRest::ckClearAllQueryParams(rest)
    CkRest::ckAddQueryParam(rest,"marketplaceids","XYZABC123")
    CkRest::ckAddQueryParam(rest,"includedData","offers")

    CkRest::ckAddHeader(rest,"x-amz-access-token","YOUR_ACCESS_TOKEN")

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

    CkAuthAws::setCkAccessKey(authAws, "YOUR_AWS_APP_ID")
    CkAuthAws::setCkSecretKey(authAws, "YOUR_AWS_APP_SECRET_KEY")
    CkAuthAws::setCkRegion(authAws, "eu-west-1")
    CkAuthAws::setCkServiceName(authAws, "execute-api")
    CkRest::ckSetAuthAws(rest,authAws)

    ; The path that is passed to FullRequestNobBody

    ; Here's a sample path that is not yet URL encoded.
    path.s = "/listings/2022-07-01/items/ABCDEFGHIJ/100x100_28g_LANCETS(BOXED)"

    ; The path passed to FullRequestNoBody needs to have the parts URL-encoded.
    ; The "/" chars are not URL encoded, but the individual path parts should be URL encoded.
    ; For example:  /listings/2022-07-01/items/ABCDEFGHIJ/100x100_28g_LANCETS%28BOXED%29

    ; In this case, we'll prepare the path like this:
    sbPath.i = CkStringBuilder::ckCreate()
    If sbPath.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbPath,"100x100_28g_LANCETS(BOXED)")
    ; URL encode the contents of the sbPath.
    CkStringBuilder::ckEncode(sbPath,"url","utf-8")
    ; Prepend the remaining which does not need to be URL encoded.
    CkStringBuilder::ckPrepend(sbPath,"/listings/2022-07-01/items/ABCDEFGHIJ/")

    Debug "URL encoded path: " + CkStringBuilder::ckGetAsString(sbPath)

    responseJson.s = CkRest::ckFullRequestNoBody(rest,"GET",CkStringBuilder::ckGetAsString(sbPath))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkStringBuilder::ckDispose(sbPath)
        ProcedureReturn
    EndIf

    Debug responseJson
    Debug "----"


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkStringBuilder::ckDispose(sbPath)


    ProcedureReturn
EndProcedure