Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbTargetCurl
    Handle hoHttpCurl
    Variant vJsonOAuth2
    Handle hoJsonOAuth2
    Variant vResponseJson
    Handle hoResponseJson
    Integer iStatusCode
    Boolean iAllTargetsDefined
    String sTemp1

    Move False To iSuccess

    // 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".

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTargetCurl
    If (Not(IsComObjectCreated(hoSbTargetCurl))) Begin
        Send CreateComObject of hoSbTargetCurl
    End
    Get ComAppendLn Of hoSbTargetCurl 'curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '  -H "Authorization: Bearer ACCESS_TOKEN" \' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '  -H "Accept: application/json"' To iSuccess

    Get Create (RefClass(cComChilkatHttpCurl)) To hoHttpCurl
    If (Not(IsComObjectCreated(hoHttpCurl))) Begin
        Send CreateComObject of hoHttpCurl
    End

    // 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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOAuth2
    If (Not(IsComObjectCreated(hoJsonOAuth2))) Begin
        Send CreateComObject of hoJsonOAuth2
    End
    Set ComEnableSecrets Of hoJsonOAuth2 To True
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_id" "!!sharepoint|oauth2|client_id" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.client_secret" "!!sharepoint|oauth2|client_secret" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.scope" "https://graph.microsoft.com/.default" To iSuccess
    Get ComUpdateString Of hoJsonOAuth2 "oauth2.token_endpoint" "!!sharepoint|oauth2|token_endpoint" To iSuccess
    Get pvComObject of hoJsonOAuth2 to vJsonOAuth2
    Get ComSetAuth Of hoHttpCurl vJsonOAuth2 To iSuccess

    // Define values for the variables used in the curl command.
    // These replace the {{sharepoint_hostname}} and {{site_name}} placeholders at runtime.
    Send ComSetVar To hoHttpCurl "sharepoint_hostname" "example.sharepoint.com"
    Send ComSetVar To hoHttpCurl "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.
    Send ComAddTargetOutput To hoHttpCurl "id" "site_id"
    Send ComAddTargetOutput To hoHttpCurl "description" "site_description"
    Send ComAddTargetOutput To hoHttpCurl "siteCollection.hostname" "site_hostname"

    // Execute the curl command. Variable substitution and authentication
    // are handled automatically.
    Get ComGetAsString Of hoSbTargetCurl To sTemp1
    Get ComDoYourThing Of hoHttpCurl sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Load the JSON response from the server.
    Get Create (RefClass(cComChilkatJsonObject)) To hoResponseJson
    If (Not(IsComObjectCreated(hoResponseJson))) Begin
        Send CreateComObject of hoResponseJson
    End
    Set ComEmitCompact Of hoResponseJson To False
    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    // Check the HTTP status code returned by the request.
    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    If (iStatusCode <> 200) Begin
        // If the request failed, the JSON response will contain error details
        // instead of the expected data.
        Get ComEmit Of hoResponseJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Verify that all target output variables were successfully extracted.
    // Passing "!" to VarDefined returns True only if all target outputs are defined.
    Get ComVarDefined Of hoHttpCurl "!" To iAllTargetsDefined
    If (iAllTargetsDefined = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Showln "Not all target outputs were located and defined."
        Procedure_Return
    End

    // Retrieve and display the extracted values from the response.
    Get ComGetVar Of hoHttpCurl "site_id" To sTemp1
    Showln "site_id = " sTemp1
    Get ComGetVar Of hoHttpCurl "site_description" To sTemp1
    Showln "site_description = " sTemp1
    Get ComGetVar Of hoHttpCurl "site_hostname" To sTemp1
    Showln "site_hostname = " sTemp1

    // 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


End_Procedure