Sample code for 30+ languages & platforms
PureBasic

curl with Target Outputs

See more CURL Examples

For curl requests that return JSON, you can define output variables that extract specific values directly from the response. Instead of manually parsing the JSON, you provide a JSON path for each value you want. Chilkat uses this path to locate the value and assign it to a variable, which your application can then retrieve using GetVar.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpCurl.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example executes a curl command to retrieve information about a SharePoint site
    ; from Microsoft Graph. The request uses variable placeholders that will be replaced
    ; at runtime with actual values.
    ; 
    ; Equivalent curl command:
    ; 
    ; curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \
    ;   -H "Authorization: Bearer ACCESS_TOKEN" \
    ;   -H "Accept: application/json"
    ; 
    ; A typical JSON response looks like this:
    ; 
    ; {
    ;   "@odata.context": "...",
    ;   "createdDateTime": "...",
    ;   "description": "Test site",
    ;   "id": "example.sharepoint.com,...",
    ;   "lastModifiedDateTime": "...",
    ;   "name": "test",
    ;   "webUrl": "...",
    ;   "displayName": "test",
    ;   "root": {},
    ;   "siteCollection": {
    ;     "hostname": "example.sharepoint.com"
    ;   }
    ; }
    ; 
    ; Rather than processing the entire JSON response, this example extracts only the
    ; specific values we care about: id, description, and siteCollection.hostname.
    ; These values are located at known JSON paths, so we can define them as "target outputs".

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

    CkStringBuilder::ckAppendLn(sbTargetCurl,"curl -X GET " + Chr(34) + "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" + Chr(34) + " \")
    CkStringBuilder::ckAppendLn(sbTargetCurl,"  -H " + Chr(34) + "Authorization: Bearer ACCESS_TOKEN" + Chr(34) + " \")
    CkStringBuilder::ckAppendLn(sbTargetCurl,"  -H " + Chr(34) + "Accept: application/json" + Chr(34))

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

    ; Configure OAuth2 authentication using the client credentials flow.
    ; Secrets (client_id, client_secret, token_endpoint) are retrieved from the
    ; local secrets manager because EnableSecrets is set to true.
    jsonOAuth2.i = CkJsonObject::ckCreate()
    If jsonOAuth2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEnableSecrets(jsonOAuth2, 1)
    CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.client_id","!!sharepoint|oauth2|client_id")
    CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.client_secret","!!sharepoint|oauth2|client_secret")
    CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.scope","https://graph.microsoft.com/.default")
    CkJsonObject::ckUpdateString(jsonOAuth2,"oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint")
    CkHttpCurl::ckSetAuth(httpCurl,jsonOAuth2)

    ; Define values for the variables used in the curl command.
    ; These replace the {{sharepoint_hostname}} and {{site_name}} placeholders at runtime.
    CkHttpCurl::ckSetVar(httpCurl,"sharepoint_hostname","example.sharepoint.com")
    CkHttpCurl::ckSetVar(httpCurl,"site_name","test")

    ; Define target outputs for the curl command.
    ; Each call maps a JSON path in the response to a variable name.
    ; After execution, these variables can be retrieved using GetVar.
    CkHttpCurl::ckAddTargetOutput(httpCurl,"id","site_id")
    CkHttpCurl::ckAddTargetOutput(httpCurl,"description","site_description")
    CkHttpCurl::ckAddTargetOutput(httpCurl,"siteCollection.hostname","site_hostname")

    ; Execute the curl command. Variable substitution and authentication
    ; are handled automatically.
    success = CkHttpCurl::ckDoYourThing(httpCurl,CkStringBuilder::ckGetAsString(sbTargetCurl))
    If success = 0
        Debug CkHttpCurl::ckLastErrorText(httpCurl)
        CkStringBuilder::ckDispose(sbTargetCurl)
        CkHttpCurl::ckDispose(httpCurl)
        CkJsonObject::ckDispose(jsonOAuth2)
        ProcedureReturn
    EndIf

    ; Load the JSON response from the server.
    responseJson.i = CkJsonObject::ckCreate()
    If responseJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(responseJson, 0)
    CkHttpCurl::ckGetResponseJson(httpCurl,responseJson)

    ; Check the HTTP status code returned by the request.
    statusCode.i = CkHttpCurl::ckStatusCode(httpCurl)
    Debug "response status code: " + Str(statusCode)

    If statusCode <> 200
        ; If the request failed, the JSON response will contain error details
        ; instead of the expected data.
        Debug CkJsonObject::ckEmit(responseJson)
        CkStringBuilder::ckDispose(sbTargetCurl)
        CkHttpCurl::ckDispose(httpCurl)
        CkJsonObject::ckDispose(jsonOAuth2)
        CkJsonObject::ckDispose(responseJson)
        ProcedureReturn
    EndIf

    ; Verify that all target output variables were successfully extracted.
    ; Passing "!" to VarDefined returns 1 only if all target outputs are defined.
    allTargetsDefined.i = CkHttpCurl::ckVarDefined(httpCurl,"!")
    If allTargetsDefined = 0
        Debug CkHttpCurl::ckLastErrorText(httpCurl)
        Debug "Not all target outputs were located and defined."
        CkStringBuilder::ckDispose(sbTargetCurl)
        CkHttpCurl::ckDispose(httpCurl)
        CkJsonObject::ckDispose(jsonOAuth2)
        CkJsonObject::ckDispose(responseJson)
        ProcedureReturn
    EndIf

    ; Retrieve and display the extracted values from the response.
    Debug "site_id = " + CkHttpCurl::ckGetVar(httpCurl,"site_id")
    Debug "site_description = " + CkHttpCurl::ckGetVar(httpCurl,"site_description")
    Debug "site_hostname = " + CkHttpCurl::ckGetVar(httpCurl,"site_hostname")

    ; Example output:

    ; site_id = example.sharepoint.com,9b923c5e-5117-44ad-8b03-cdbb8e19ae85,b2451e19-290f-4f29-9f5d-674c2951a9f7
    ; site_description = Test site
    ; site_hostname = example.sharepoint.com


    CkStringBuilder::ckDispose(sbTargetCurl)
    CkHttpCurl::ckDispose(httpCurl)
    CkJsonObject::ckDispose(jsonOAuth2)
    CkJsonObject::ckDispose(responseJson)


    ProcedureReturn
EndProcedure