Sample code for 30+ languages & platforms
Unicode C

Yousign Procedure Creation Basic Mode Upload File

See more Yousign Examples

Demonstrates the 1st step of Yousign procedure creation, which is to upload a PDF file.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkBinDataW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkBinDataW pdfData;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *id;
    const wchar_t *name;
    const wchar_t *v_type;
    const wchar_t *contentType;
    const wchar_t *description;
    const wchar_t *createdAt;
    const wchar_t *updatedAt;
    const wchar_t *sha256;
    const wchar_t *workspace;
    const wchar_t *creator;
    BOOL v_protected;
    int position;
    const wchar_t *parent;
    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 = CkHttpW_Create();

    // Implements the following CURL command:

    // curl --location --request POST 'https://staging-api.yousign.com/files' \
    // --header 'Authorization: Bearer YOUR_API_KEY' \
    // --header 'Content-Type: application/json' \
    // --data-raw '{
    //     "name": "The best name for my file.pdf",
    //     "content": "JVBERi0..." }'

    // Load a PDF file to be uploaded.
    pdfData = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(pdfData,L"qa_data/pdf/helloWorld.pdf");
    if (success == FALSE) {
        wprintf(L"Failed to load PDF local file.\n");
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(pdfData);
        return;
    }

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

    // {
    //   "name": "The best name for my file.pdf",
    //   "content": "JVBERi0..."
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"name",L"helloWorld.pdf");
    CkJsonObjectW_UpdateString(json,L"content",CkBinDataW_getEncoded(pdfData,L"base64"));

    // Adds the "Authorization: Bearer YOUR_API_KEY" header.
    CkHttpW_putAuthToken(http,L"YOUR_API_KEY");

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://staging-api.yousign.com/files",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(pdfData);
        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);
        CkBinDataW_Dispose(pdfData);
        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)

    // {
    //   "id": "/files/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    //   "name": "The best name for my file.pdf",
    //   "type": "signable",
    //   "contentType": "application/pdf",
    //   "description": null,
    //   "createdAt": "2018-12-01T11:36:20+01:00",
    //   "updatedAt": "2018-12-01T11:36:20+01:00",
    //   "sha256": "bb57ae2b2ca6ad0133a699350d1a6f6c8cdfde3cf872cf526585d306e4675cc2",
    //   "metadata": [
    //   ],
    //   "workspace": "/workspaces/XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    //   "creator": null,
    //   "protected": false,
    //   "position": 0,
    //   "parent": 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.

    id = CkJsonObjectW_stringOf(jResp,L"id");
    name = CkJsonObjectW_stringOf(jResp,L"name");
    v_type = CkJsonObjectW_stringOf(jResp,L"type");
    contentType = CkJsonObjectW_stringOf(jResp,L"contentType");
    description = CkJsonObjectW_stringOf(jResp,L"description");
    createdAt = CkJsonObjectW_stringOf(jResp,L"createdAt");
    updatedAt = CkJsonObjectW_stringOf(jResp,L"updatedAt");
    sha256 = CkJsonObjectW_stringOf(jResp,L"sha256");
    workspace = CkJsonObjectW_stringOf(jResp,L"workspace");
    creator = CkJsonObjectW_stringOf(jResp,L"creator");
    v_protected = CkJsonObjectW_BoolOf(jResp,L"protected");
    position = CkJsonObjectW_IntOf(jResp,L"position");
    parent = CkJsonObjectW_stringOf(jResp,L"parent");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"metadata");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkBinDataW_Dispose(pdfData);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }