Chilkat Online Tools

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

Back to Collection Items

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

Dim http As New ChilkatHttp
Dim success As Long

Dim bdRequestBody As New ChilkatBinData
success = bdRequestBody.LoadFile("file")
If (success <> 1) Then
    Debug.Print "Failed to load file"
    Exit Sub
End If

http.SetRequestHeader "Accept","application/json"

Dim resp As ChilkatHttpResponse
Set resp = http.PBinaryBd("POST","https://ksef.mf.gov.pl/api/online/Session/InitSigned",bdRequestBody,"application/octet-stream",0,0)
If (http.LastMethodSuccess = 0) Then
    Debug.Print http.LastErrorText
    Exit Sub
End If

Dim sbResponseBody As New ChilkatStringBuilder
success = resp.GetBodySb(sbResponseBody)

Dim jResp As New ChilkatJsonObject
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = 0

Debug.Print "Response Body:"
Debug.Print jResp.Emit()

Dim respStatusCode As Long
respStatusCode = resp.StatusCode
Debug.Print "Response Status Code = " & respStatusCode
If (respStatusCode >= 400) Then
    Debug.Print "Response Header:"
    Debug.Print resp.Header
    Debug.Print "Failed."

    Exit Sub
End If

' 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

Dim startTimestamp As String
Dim v_type As String
Dim roleDescription As String

Dim referenceNumber As String
referenceNumber = jResp.StringOf("referenceNumber")
Dim v_Type As String
v_Type = jResp.StringOf("sessionToken.context.contextIdentifier.type")
Dim ContextNameType As String
ContextNameType = jResp.StringOf("sessionToken.context.contextName.type")
Dim TradeName As String
TradeName = jResp.StringOf("sessionToken.context.contextName.tradeName")
Dim Token As String
Token = jResp.StringOf("sessionToken.token")
Dim timestamp As String
timestamp = jResp.StringOf("timestamp")
Dim i As Long
i = 0
Dim count_i As Long
count_i = jResp.SizeOfArray("sessionToken.context.credentialsRoleList")
Do While i < count_i
    jResp.I = i
    startTimestamp = jResp.StringOf("sessionToken.context.credentialsRoleList[i].startTimestamp")
    v_type = jResp.StringOf("sessionToken.context.credentialsRoleList[i].type")
    roleDescription = jResp.StringOf("sessionToken.context.credentialsRoleList[i].roleDescription")
    i = i + 1
Loop

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}"
    }
  ]
}