Chilkat Online Tools

unicodeC / KSeF / Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkBinDataW bdRequestBody;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *startTimestamp;
    const wchar_t *v_type;
    const wchar_t *roleDescription;
    const wchar_t *referenceNumber;
    const wchar_t *v_Type;
    const wchar_t *ContextNameType;
    const wchar_t *TradeName;
    const wchar_t *Token;
    const wchar_t *timestamp;
    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();

    bdRequestBody = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(bdRequestBody,L"file");
    if (success != TRUE) {
        wprintf(L"Failed to load file\n");
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bdRequestBody);
        return;
    }

    CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");

    resp = CkHttpW_PBinaryBd(http,L"POST",L"https://ksef.mf.gov.pl/api/online/Session/InitSigned",bdRequestBody,L"application/octet-stream",FALSE,FALSE);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bdRequestBody);
        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);
        CkBinDataW_Dispose(bdRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "referenceNumber": "28201005-FY-586FD2AD02-40A998FD15-B2",
    //   "sessionToken": {
    //     "context": {
    //       "contextIdentifier": {
    //         "type": "<string>"
    //       },
    //       "credentialsRoleList": [
    //         {
    //           "startTimestamp": "<dateTime>",
    //           "type": "<string>",
    //           "roleDescription": "<string>",
    //           "roleType": {}
    //         }
    //       ],
    //       "contextName": {
    //         "type": "<string>",
    //         "tradeName": "<string>"
    //       }
    //     },
    //     "token": "<string>"
    //   },
    //   "timestamp": "<dateTime>"
    // }

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

    referenceNumber = CkJsonObjectW_stringOf(jResp,L"referenceNumber");
    v_Type = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.contextIdentifier.type");
    ContextNameType = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.contextName.type");
    TradeName = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.contextName.tradeName");
    Token = CkJsonObjectW_stringOf(jResp,L"sessionToken.token");
    timestamp = CkJsonObjectW_stringOf(jResp,L"timestamp");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"sessionToken.context.credentialsRoleList");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        startTimestamp = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.credentialsRoleList[i].startTimestamp");
        v_type = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.credentialsRoleList[i].type");
        roleDescription = CkJsonObjectW_stringOf(jResp,L"sessionToken.context.credentialsRoleList[i].roleDescription");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkBinDataW_Dispose(bdRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Content-Type: application/octet-stream"
	-H "Accept: application/json"
	--data-binary '@file'
https://ksef.mf.gov.pl/api/online/Session/InitSigned

Postman Collection Item JSON

{
  "name": "Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/octet-stream"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "file",
      "file": {}
    },
    "url": {
      "raw": "{{baseUrl}}/online/Session/InitSigned",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "online",
        "Session",
        "InitSigned"
      ]
    },
    "description": "Inicjalizacja sesji interaktywnej. Podpisany dokument http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001/InitSessionSignedRequest"
  },
  "response": [
    {
      "name": "Inicjalizacja sesji interaktywnej poprawna",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/octet-stream"
          },
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "file",
          "file": {}
        },
        "url": {
          "raw": "{{baseUrl}}/online/Session/InitSigned",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitSigned"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"referenceNumber\": \"28201005-FY-586FD2AD02-40A998FD15-B2\",\n  \"sessionToken\": {\n    \"context\": {\n      \"contextIdentifier\": {\n        \"type\": \"<string>\"\n      },\n      \"credentialsRoleList\": [\n        {\n          \"startTimestamp\": \"<dateTime>\",\n          \"type\": \"<string>\",\n          \"roleDescription\": \"<string>\",\n          \"roleType\": {}\n        }\n      ],\n      \"contextName\": {\n        \"type\": \"<string>\",\n        \"tradeName\": \"<string>\"\n      }\n    },\n    \"token\": \"<string>\"\n  },\n  \"timestamp\": \"<dateTime>\"\n}"
    },
    {
      "name": "Nieprawidłowe wywołanie",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/octet-stream"
          },
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "file",
          "file": {}
        },
        "url": {
          "raw": "{{baseUrl}}/online/Session/InitSigned",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitSigned"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"exception\": {\n    \"exceptionDetailList\": [\n      {\n        \"exceptionCode\": \"<integer>\",\n        \"exceptionDescription\": \"<string>\"\n      }\n    ],\n    \"serviceCode\": \"<string>\",\n    \"serviceCtx\": \"<string>\",\n    \"serviceName\": \"<string>\",\n    \"timestamp\": \"<dateTime>\",\n    \"referenceNumber\": \"20560508-TX-BC1107E227-31DE9417C0-CE\"\n  }\n}"
    }
  ]
}