Back to Collection Items
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
HCkHttpW http;
BOOL success;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *graphId;
BOOL isSuccessful;
int j;
int count_j;
const wchar_t *Id;
BOOL Success;
const wchar_t *Location;
int httpStatusCode;
const wchar_t *referenceId;
int k;
int count_k;
int i;
int count_i;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "graphs": [
// {
// "graphId": "graph1",
// "compositeRequest": [
// {
// "method": "POST",
// "url": "/services/data/v{{version}}/sobjects/Account/",
// "referenceId": "newAccount",
// "body": {
// "Name": "ACME Inc.",
// "Custom__c": "Hello world"
// }
// },
// {
// "method": "GET",
// "url": "/services/data/v{{version}}/sobjects/Account/@{newAccount.id}",
// "referenceId": "newAccountInfo"
// }
// ]
// }
// ]
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"graphs[0].graphId",L"graph1");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[0].method",L"POST");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[0].url",L"/services/data/v{{version}}/sobjects/Account/");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[0].referenceId",L"newAccount");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[0].body.Name",L"ACME Inc.");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[0].body.Custom__c",L"Hello world");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[1].method",L"GET");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[1].url",L"/services/data/v{{version}}/sobjects/Account/@{newAccount.id}");
CkJsonObjectW_UpdateString(json,L"graphs[0].compositeRequest[1].referenceId",L"newAccountInfo");
// Adds the "Authorization: Bearer <access_token>" header.
CkHttpW_putAuthToken(http,L"<access_token>");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
resp = CkHttpW_PostJson3(http,L"https://domain.com/services/data/v{{version}}/composite/graph",L"application/json",json);
if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpResponseW_Dispose(resp);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
CkHttpResponseW_Dispose(resp);
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "graphs": [
// {
// "graphId": "graph1",
// "graphResponse": {
// "compositeResponse": [
// {
// "body": {
// "id": "0014H00002x6HUhQAM",
// "success": true,
// "errors": [
// ]
// },
// "httpHeaders": {
// "Location": "/services/data/v50.0/sobjects/Account/0014H00002x6HUhQAM"
// },
// "httpStatusCode": 201,
// "referenceId": "newAccount"
// },
// {
// "body": {
// "id": "0034H00002JsInQQAV",
// "success": true,
// "errors": [
// ]
// },
// "httpHeaders": {
// "Location": "/services/data/v50.0/sobjects/Contact/0034H00002JsInQQAV"
// },
// "httpStatusCode": 201,
// "referenceId": "newContact"
// }
// ]
// },
// "isSuccessful": true
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"graphs");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
graphId = CkJsonObjectW_stringOf(jResp,L"graphs[i].graphId");
isSuccessful = CkJsonObjectW_BoolOf(jResp,L"graphs[i].isSuccessful");
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"graphs[i].graphResponse.compositeResponse");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
Id = CkJsonObjectW_stringOf(jResp,L"graphs[i].graphResponse.compositeResponse[j].body.id");
Success = CkJsonObjectW_BoolOf(jResp,L"graphs[i].graphResponse.compositeResponse[j].body.success");
Location = CkJsonObjectW_stringOf(jResp,L"graphs[i].graphResponse.compositeResponse[j].httpHeaders.Location");
httpStatusCode = CkJsonObjectW_IntOf(jResp,L"graphs[i].graphResponse.compositeResponse[j].httpStatusCode");
referenceId = CkJsonObjectW_stringOf(jResp,L"graphs[i].graphResponse.compositeResponse[j].referenceId");
k = 0;
count_k = CkJsonObjectW_SizeOfArray(jResp,L"graphs[i].graphResponse.compositeResponse[j].body.errors");
while (k < count_k) {
CkJsonObjectW_putK(jResp,k);
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
-d '{
"graphs": [
{
"graphId": "graph1",
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v{{version}}/sobjects/Account/",
"referenceId": "newAccount",
"body": {
"Name": "ACME Inc.",
"Custom__c": "Hello world"
}
},
{
"method": "GET",
"url": "/services/data/v{{version}}/sobjects/Account/@{newAccount.id}",
"referenceId": "newAccountInfo"
}
]
}
]
}'
https://domain.com/services/data/v{{version}}/composite/graph
Postman Collection Item JSON
{
"name": "Composite Graph",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"graphs\": [\n {\n \"graphId\": \"graph1\",\n \"compositeRequest\": [\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v{{version}}/sobjects/Account/\",\n \"referenceId\": \"newAccount\",\n \"body\": {\n \"Name\": \"ACME Inc.\",\n \"Custom__c\": \"Hello world\"\n }\n },\n {\n \"method\": \"GET\",\n \"url\": \"/services/data/v{{version}}/sobjects/Account/@{newAccount.id}\",\n \"referenceId\": \"newAccountInfo\"\n }\n ]\n }\n ]\n}"
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/composite/graph",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"composite",
"graph"
]
},
"description": "Composite graphs provide an enhanced way to perform composite requests, which execute a series of REST API requests in a single call.\nRegular composite requests allow you to execute a series of REST API requests in a single call. And you can use the output of one request as the input to a subsequent request.\n\nComposite graphs extend this by allowing you to assemble a more complicated and complete series of related objects and records.\n\nComposite graphs also enable you to ensure that the steps in a given set of operations are either all completed or all not completed. This avoids requiring you to check for a mix of successful and unsuccessful results.\n\nRegular composite requests have a limit of 25 subrequests. Composite graphs increase this limit to 500. This gives a single API call much greater power.\n\nhttps://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_graph_introduction.htm"
},
"response": [
{
"name": "Successful Composite Graph",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"graphs\": [\n {\n \"graphId\": \"graph1\",\n \"compositeRequest\": [\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v{{version}}/sobjects/Account/\",\n \"referenceId\": \"newAccount\",\n \"body\": {\n \"Name\": \"ACME Inc.\"\n }\n },\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v{{version}}/sobjects/Contact/\",\n \"referenceId\": \"newContact\",\n \"body\": {\n \"FirstName\": \"John\",\n \"LastName\": \"Doe\",\n \"AccountId\": \"@{newAccount.id}\"\n }\n }\n ]\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/composite/graph",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"composite",
"graph"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "cache-control",
"value": "no-cache,must-revalidate,max-age=0,no-store,private"
},
{
"key": "content-type",
"value": "application/json;charset=UTF-8"
}
],
"cookie": [
],
"body": "{\n \"graphs\": [\n {\n \"graphId\": \"graph1\",\n \"graphResponse\": {\n \"compositeResponse\": [\n {\n \"body\": {\n \"id\": \"0014H00002x6HUhQAM\",\n \"success\": true,\n \"errors\": []\n },\n \"httpHeaders\": {\n \"Location\": \"/services/data/v50.0/sobjects/Account/0014H00002x6HUhQAM\"\n },\n \"httpStatusCode\": 201,\n \"referenceId\": \"newAccount\"\n },\n {\n \"body\": {\n \"id\": \"0034H00002JsInQQAV\",\n \"success\": true,\n \"errors\": []\n },\n \"httpHeaders\": {\n \"Location\": \"/services/data/v50.0/sobjects/Contact/0034H00002JsInQQAV\"\n },\n \"httpStatusCode\": 201,\n \"referenceId\": \"newContact\"\n }\n ]\n },\n \"isSuccessful\": true\n }\n ]\n}"
}
]
}