Visual FoxPro
Visual FoxPro
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 usingGetVar.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSbTargetCurl
LOCAL loHttpCurl
LOCAL loJsonOAuth2
LOCAL loResponseJson
LOCAL lnStatusCode
LOCAL lnAllTargetsDefined
lnSuccess = 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".
loSbTargetCurl = CreateObject('Chilkat.StringBuilder')
loSbTargetCurl.AppendLn('curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \')
loSbTargetCurl.AppendLn(' -H "Authorization: Bearer ACCESS_TOKEN" \')
loSbTargetCurl.AppendLn(' -H "Accept: application/json"')
loHttpCurl = CreateObject('Chilkat.HttpCurl')
* 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.
loJsonOAuth2 = CreateObject('Chilkat.JsonObject')
loJsonOAuth2.EnableSecrets = 1
loJsonOAuth2.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id")
loJsonOAuth2.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret")
loJsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default")
loJsonOAuth2.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint")
loHttpCurl.SetAuth(loJsonOAuth2)
* Define values for the variables used in the curl command.
* These replace the {{sharepoint_hostname}} and {{site_name}} placeholders at runtime.
loHttpCurl.SetVar("sharepoint_hostname","example.sharepoint.com")
loHttpCurl.SetVar("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.
loHttpCurl.AddTargetOutput("id","site_id")
loHttpCurl.AddTargetOutput("description","site_description")
loHttpCurl.AddTargetOutput("siteCollection.hostname","site_hostname")
* Execute the curl command. Variable substitution and authentication
* are handled automatically.
lnSuccess = loHttpCurl.DoYourThing(loSbTargetCurl.GetAsString())
IF (lnSuccess = 0) THEN
? loHttpCurl.LastErrorText
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loJsonOAuth2
CANCEL
ENDIF
* Load the JSON response from the server.
loResponseJson = CreateObject('Chilkat.JsonObject')
loResponseJson.EmitCompact = 0
loHttpCurl.GetResponseJson(loResponseJson)
* Check the HTTP status code returned by the request.
lnStatusCode = loHttpCurl.StatusCode
? "response status code: " + STR(lnStatusCode)
IF (lnStatusCode <> 200) THEN
* If the request failed, the JSON response will contain error details
* instead of the expected data.
? loResponseJson.Emit()
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loJsonOAuth2
RELEASE loResponseJson
CANCEL
ENDIF
* Verify that all target output variables were successfully extracted.
* Passing "!" to VarDefined returns 1 only if all target outputs are defined.
lnAllTargetsDefined = loHttpCurl.VarDefined("!")
IF (lnAllTargetsDefined = 0) THEN
? loHttpCurl.LastErrorText
? "Not all target outputs were located and defined."
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loJsonOAuth2
RELEASE loResponseJson
CANCEL
ENDIF
* Retrieve and display the extracted values from the response.
? "site_id = " + loHttpCurl.GetVar("site_id")
? "site_description = " + loHttpCurl.GetVar("site_description")
? "site_hostname = " + loHttpCurl.GetVar("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
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loJsonOAuth2
RELEASE loResponseJson