Chilkat Online Tools

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

Back to Collection Items

LOCAL loHttp
LOCAL lnSuccess
LOCAL loBdRequestBody
LOCAL loResp
LOCAL loSbResponseBody
LOCAL loJResp
LOCAL lnRespStatusCode
LOCAL lcStartTimestamp
LOCAL lcV_type
LOCAL lcRoleDescription
LOCAL lcReferenceNumber
LOCAL lcV_Type
LOCAL lcContextNameType
LOCAL lcTradeName
LOCAL lcToken
LOCAL lcTimestamp
LOCAL i
LOCAL lnCount_i

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

* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Http')
loHttp = CreateObject('Chilkat.Http')

* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.BinData')
loBdRequestBody = CreateObject('Chilkat.BinData')
lnSuccess = loBdRequestBody.LoadFile("file")
IF (lnSuccess <> 1) THEN
    ? "Failed to load file"
    RELEASE loHttp
    RELEASE loBdRequestBody
    CANCEL
ENDIF

loHttp.SetRequestHeader("Accept","application/json")

loResp = loHttp.PBinaryBd("POST","https://ksef.mf.gov.pl/api/online/Session/InitToken",loBdRequestBody,"application/octet-stream",0,0)
IF (loHttp.LastMethodSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loBdRequestBody
    CANCEL
ENDIF

* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder')
loSbResponseBody = CreateObject('Chilkat.StringBuilder')
loResp.GetBodySb(loSbResponseBody)

* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject')
loJResp = CreateObject('Chilkat.JsonObject')
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = 0

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + STR(lnRespStatusCode)
IF (lnRespStatusCode >= 400) THEN
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    RELEASE loResp
    RELEASE loHttp
    RELEASE loBdRequestBody
    RELEASE loSbResponseBody
    RELEASE loJResp
    CANCEL
ENDIF

RELEASE loResp

* 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

lcReferenceNumber = loJResp.StringOf("referenceNumber")
lcV_Type = loJResp.StringOf("sessionToken.context.contextIdentifier.type")
lcContextNameType = loJResp.StringOf("sessionToken.context.contextName.type")
lcTradeName = loJResp.StringOf("sessionToken.context.contextName.tradeName")
lcToken = loJResp.StringOf("sessionToken.token")
lcTimestamp = loJResp.StringOf("timestamp")
i = 0
lnCount_i = loJResp.SizeOfArray("sessionToken.context.credentialsRoleList")
DO WHILE i < lnCount_i
    loJResp.I = i
    lcStartTimestamp = loJResp.StringOf("sessionToken.context.credentialsRoleList[i].startTimestamp")
    lcV_type = loJResp.StringOf("sessionToken.context.credentialsRoleList[i].type")
    lcRoleDescription = loJResp.StringOf("sessionToken.context.credentialsRoleList[i].roleDescription")
    i = i + 1
ENDDO

RELEASE loHttp
RELEASE loBdRequestBody
RELEASE loSbResponseBody
RELEASE loJResp

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

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/InitToken",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "online",
        "Session",
        "InitToken"
      ]
    },
    "description": "Inicjalizacja sesji interaktywnej. Zaszyfrowany kluczem publicznym KSeF dokument http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001/InitSessionTokenRequest"
  },
  "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/InitToken",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitToken"
          ]
        }
      },
      "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/InitToken",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitToken"
          ]
        }
      },
      "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}"
    }
  ]
}