Sample code for 30+ languages & platforms
C

SMSAPI - Create a Contact

See more SMSAPI Examples

Create a Contact

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkDtObj.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkHttpRequest req;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    HCkDtObj date_created;
    HCkDtObj date_updated;
    const char *name;
    const char *created_by;
    const char *idx;
    const char *contact_expire_after;
    const char *contacts_count;
    const char *id;
    const char *first_name;
    const char *last_name;
    const char *phone_number;
    const char *email;
    const char *gender;
    const char *city;
    const char *description;
    int i;
    int count_i;

    success = FALSE;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttp_Create();

    // Implements the following CURL command:

    // curl -X POST https://api.smsapi.com/contacts -H "Authorization: Bearer token_api_oauth" \
    // -d "phone_number=48500000000&email=bok@smsapi.com&first_name=Name&last_name=Last_name&gender=gender&description=description&city=city&groups=default"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    req = CkHttpRequest_Create();
    CkHttpRequest_putHttpVerb(req,"POST");
    CkHttpRequest_putPath(req,"/contacts");
    CkHttpRequest_putContentType(req,"application/x-www-form-urlencoded");
    CkHttpRequest_AddParam(req,"phone_number","48500000000");
    CkHttpRequest_AddParam(req,"email","bok@smsapi.com");
    CkHttpRequest_AddParam(req,"first_name","Name");
    CkHttpRequest_AddParam(req,"last_name","Last_name");
    CkHttpRequest_AddParam(req,"gender","gender");
    CkHttpRequest_AddParam(req,"description","description");
    CkHttpRequest_AddParam(req,"city","city");
    CkHttpRequest_AddParam(req,"groups","default");

    CkHttpRequest_AddHeader(req,"Authorization","Bearer token_api_oauth");

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpReq(http,"https://api.smsapi.com/contacts",req,resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        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");
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkHttpResponse_Dispose(resp);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "id": "5b802315a788494a04690d1d",
    //   "first_name": "string",
    //   "last_name": "string",
    //   "phone_number": "48500000000",
    //   "email": "bok@smsapi.com",
    //   "gender": "gender",
    //   "city": "city",
    //   "date_created": "2018-08-24T17:24:05+02:00",
    //   "date_updated": "2018-08-24T17:24:05+02:00",
    //   "description": "description",
    //   "groups": [
    //     {
    //       "id": "59a3ca1fa78849062837cd0c",
    //       "name": "default",
    //       "date_created": "2017-08-28T09:45:35+02:00",
    //       "date_updated": "2017-08-28T09:45:35+02:00",
    //       "description": "",
    //       "created_by": "username",
    //       "idx": null,
    //       "contact_expire_after": null,
    //       "contacts_count": null
    //     }
    //   ]
    // }

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

    date_created = CkDtObj_Create();
    date_updated = CkDtObj_Create();

    id = CkJsonObject_stringOf(jResp,"id");
    first_name = CkJsonObject_stringOf(jResp,"first_name");
    last_name = CkJsonObject_stringOf(jResp,"last_name");
    phone_number = CkJsonObject_stringOf(jResp,"phone_number");
    email = CkJsonObject_stringOf(jResp,"email");
    gender = CkJsonObject_stringOf(jResp,"gender");
    city = CkJsonObject_stringOf(jResp,"city");
    CkJsonObject_DtOf(jResp,"date_created",FALSE,date_created);
    CkJsonObject_DtOf(jResp,"date_updated",FALSE,date_updated);
    description = CkJsonObject_stringOf(jResp,"description");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"groups");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"groups[i].id");
        name = CkJsonObject_stringOf(jResp,"groups[i].name");
        CkJsonObject_DtOf(jResp,"groups[i].date_created",FALSE,date_created);
        CkJsonObject_DtOf(jResp,"groups[i].date_updated",FALSE,date_updated);
        description = CkJsonObject_stringOf(jResp,"groups[i].description");
        created_by = CkJsonObject_stringOf(jResp,"groups[i].created_by");
        idx = CkJsonObject_stringOf(jResp,"groups[i].idx");
        contact_expire_after = CkJsonObject_stringOf(jResp,"groups[i].contact_expire_after");
        contacts_count = CkJsonObject_stringOf(jResp,"groups[i].contacts_count");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkHttpRequest_Dispose(req);
    CkHttpResponse_Dispose(resp);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);
    CkDtObj_Dispose(date_created);
    CkDtObj_Dispose(date_updated);

    }