Back to Collection Items
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
HCkHttp http;
BOOL success;
HCkJsonObject json;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *v_type;
const char *clientId;
const char *Id;
const char *UserId;
int UnreadCount;
const char *UserExternalId;
const char *LastRead;
int i;
int count_i;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
CkHttp_putBasicAuth(http,TRUE);
CkHttp_putLogin(http,"username");
CkHttp_putPassword(http,"password");
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "userId": "42589ad070d43be9b00ff7e5",
// "subscribeSDKClient": false
// }
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"userId","42589ad070d43be9b00ff7e5");
CkJsonObject_UpdateBool(json,"subscribeSDKClient",FALSE);
CkHttp_SetRequestHeader(http,"Content-Type","application/json");
resp = CkHttp_PostJson3(http,"https://domain.com/","application/json",json);
if (CkHttp_getLastMethodSuccess(http) == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttpResponse_Dispose(resp);
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
CkHttpResponse_Dispose(resp);
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "participant": {
// "id": "c93bb9c14dde8ffb94564eae",
// "userId": "42589ad070d43be9b00ff7e5",
// "unreadCount": 0,
// "clientAssociations": [
// {
// "type": "telegram",
// "clientId": "42589ad070d43be9b00ff7e5"
// },
// {
// "type": "telegram",
// "clientId": "42589ad070d43be9b00ff7e5"
// }
// ],
// "userExternalId": "your-own-id",
// "lastRead": "2019-01-14T18:55:12.515Z"
// }
// }
// 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.
Id = CkJsonObject_stringOf(jResp,"participant.id");
UserId = CkJsonObject_stringOf(jResp,"participant.userId");
UnreadCount = CkJsonObject_IntOf(jResp,"participant.unreadCount");
UserExternalId = CkJsonObject_stringOf(jResp,"participant.userExternalId");
LastRead = CkJsonObject_stringOf(jResp,"participant.lastRead");
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"participant.clientAssociations");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
v_type = CkJsonObject_stringOf(jResp,"participant.clientAssociations[i].type");
clientId = CkJsonObject_stringOf(jResp,"participant.clientAssociations[i].clientId");
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}
Curl Command
curl -X POST
-u 'username:password'
-H "Content-Type: application/json"
-d '{
"userId": "42589ad070d43be9b00ff7e5",
"subscribeSDKClient": false
}'
https://domain.com/
Postman Collection Item JSON
{
"name": "Join Conversation",
"_postman_id": "185b1369-9a43-41a4-a274-81fa30f2a53b",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"userId\": \"42589ad070d43be9b00ff7e5\",\n \"subscribeSDKClient\": false\n}"
},
"url": "{{url}}/v2/apps/{{appId}}/conversations/{{conversationId}}/join",
"description": "Adds a user to a conversation using either their userId or userExternalId. The endpoint only\nsupports adding a participant to a sdkGroup conversation.\n"
},
"response": [
{
"id": "019219b1-cf37-47f5-856f-f8bcace7ac4d",
"name": "Too many participants",
"originalRequest": {
"method": "POST",
"header": [
{
"description": {
"content": "Added as a part of security scheme: bearer",
"type": "text/plain"
},
"key": "Authorization",
"value": "Bearer <token>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"userId\": \"42589ad070d43be9b00ff7e5\",\n \"subscribeSDKClient\": false\n}"
},
"url": {
"raw": "{{baseUrl}}/v2/apps/:appId/conversations/:conversationId/join",
"host": [
"{{baseUrl}}"
],
"path": [
"v2",
"apps",
":appId",
"conversations",
":conversationId",
"join"
],
"variable": [
{
"key": "appId"
},
{
"key": "conversationId"
}
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "text",
"header": [
{
"key": "Content-Type",
"value": "text/plain"
}
],
"cookie": [
],
"body": ""
},
{
"id": "30b353f8-bbca-47db-b21e-97218a2d50f0",
"name": "Created",
"originalRequest": {
"method": "POST",
"header": [
{
"description": {
"content": "Added as a part of security scheme: bearer",
"type": "text/plain"
},
"key": "Authorization",
"value": "Bearer <token>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"userId\": \"42589ad070d43be9b00ff7e5\",\n \"subscribeSDKClient\": false\n}"
},
"url": {
"raw": "{{baseUrl}}/v2/apps/:appId/conversations/:conversationId/join",
"host": [
"{{baseUrl}}"
],
"path": [
"v2",
"apps",
":appId",
"conversations",
":conversationId",
"join"
],
"variable": [
{
"key": "appId"
},
{
"key": "conversationId"
}
]
}
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [
],
"body": "{\n \"participant\": {\n \"id\": \"c93bb9c14dde8ffb94564eae\",\n \"userId\": \"42589ad070d43be9b00ff7e5\",\n \"unreadCount\": 0,\n \"clientAssociations\": [\n {\n \"type\": \"telegram\",\n \"clientId\": \"42589ad070d43be9b00ff7e5\"\n },\n {\n \"type\": \"telegram\",\n \"clientId\": \"42589ad070d43be9b00ff7e5\"\n }\n ],\n \"userExternalId\": \"your-own-id\",\n \"lastRead\": \"2019-01-14T18:55:12.515Z\"\n }\n}"
},
{
"id": "a12e66e5-6748-4f9a-87a3-a5cefad55a7a",
"name": "Conversation not found",
"originalRequest": {
"method": "POST",
"header": [
{
"description": {
"content": "Added as a part of security scheme: bearer",
"type": "text/plain"
},
"key": "Authorization",
"value": "Bearer <token>"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"userId\": \"42589ad070d43be9b00ff7e5\",\n \"subscribeSDKClient\": false\n}"
},
"url": {
"raw": "{{baseUrl}}/v2/apps/:appId/conversations/:conversationId/join",
"host": [
"{{baseUrl}}"
],
"path": [
"v2",
"apps",
":appId",
"conversations",
":conversationId",
"join"
],
"variable": [
{
"key": "appId"
},
{
"key": "conversationId"
}
]
}
},
"status": "Not Found",
"code": 404,
"_postman_previewlanguage": "text",
"header": [
{
"key": "Content-Type",
"value": "text/plain"
}
],
"cookie": [
],
"body": ""
}
]
}