Unicode C
Unicode C
SMSAPI - Add Subuser Account
See more SMSAPI Examples
Adds a new sub-user account.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *username;
int limit;
int month_limit;
int senders;
int phonebook;
BOOL active;
const wchar_t *info;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Implements the following CURL command:
// curl https://api.smsapi.com/subusers -i -H "Authorization: Bearer token_api_oauth" \
// -H "Content-Type: application/json" -X POST \
// -d '{"credentials": { "username": "User_name","password" :"Smsapi_panel_password","api_password":"api_password"},
// "active":"1","description":"description","points":{"from_account":"2","per_month":"2"}}'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "credentials": {
// "username": "User_name",
// "password": "Smsapi_panel_password",
// "api_password": "api_password"
// },
// "active": "1",
// "description": "description",
// "points": {
// "from_account": "2",
// "per_month": "2"
// }
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"credentials.username",L"User_name");
CkJsonObjectW_UpdateString(json,L"credentials.password",L"Smsapi_panel_password123");
CkJsonObjectW_UpdateString(json,L"credentials.api_password",L"api_Password123");
CkJsonObjectW_UpdateString(json,L"active",L"1");
CkJsonObjectW_UpdateString(json,L"description",L"description");
CkJsonObjectW_UpdateString(json,L"points.from_account",L"2");
CkJsonObjectW_UpdateString(json,L"points.per_month",L"2");
// Adds the "Authorization: Bearer token_api_oauth" header.
CkHttpW_putAuthToken(http,L"token_api_oauth");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpJson(http,L"POST",L"https://api.smsapi.com/subusers",json,L"application/json",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
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");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "username": "subuser",
// "limit": 0,
// "month_limit": 0,
// "senders": 0,
// "phonebook": 0,
// "active": false,
// "info": "unknown"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
username = CkJsonObjectW_stringOf(jResp,L"username");
limit = CkJsonObjectW_IntOf(jResp,L"limit");
month_limit = CkJsonObjectW_IntOf(jResp,L"month_limit");
senders = CkJsonObjectW_IntOf(jResp,L"senders");
phonebook = CkJsonObjectW_IntOf(jResp,L"phonebook");
active = CkJsonObjectW_BoolOf(jResp,L"active");
info = CkJsonObjectW_stringOf(jResp,L"info");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}