Chilkat Online Tools

PureBasic / Broker API / Document

Back to Collection Items

IncludeFile "CkJsonArray.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "{{api_key}}")
    CkHttp::setCkPassword(http, "{{api_secret}}")

    ; Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; [
    ;   {
    ;     "document_type": "cip_result",
    ;     "content": "VGhlcmUgYXJlIG5vIHdpbGQgYWxwYWNhcy4=",
    ;     "mime_type": "application/pdf"
    ;   },
    ;   {
    ;     "document_type": "identity_verification",
    ;     "document_sub_type": "passport",
    ;     "content": "QWxwYWNhcyBjYW5ub3QgbGl2ZSBhbG9uZS4=",
    ;     "mime_type": "image/jpeg"
    ;   }
    ; ]

    jarr.i = CkJsonArray::ckCreate()
    If jarr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddObjectAt(jarr,-1)
    jsonObj_1.i = CkJsonArray::ckObjectAt(jarr,CkJsonArray::ckSize(jarr) - 1)
    CkJsonObject::ckUpdateString(jsonObj_1,"document_type","cip_result")
    CkJsonObject::ckUpdateString(jsonObj_1,"content","VGhlcmUgYXJlIG5vIHdpbGQgYWxwYWNhcy4=")
    CkJsonObject::ckUpdateString(jsonObj_1,"mime_type","application/pdf")
    CkJsonObject::ckDispose(jsonObj_1)

    CkJsonArray::ckAddObjectAt(jarr,-1)
    jsonObj_1 = CkJsonArray::ckObjectAt(jarr,CkJsonArray::ckSize(jarr) - 1)
    CkJsonObject::ckUpdateString(jsonObj_1,"document_type","identity_verification")
    CkJsonObject::ckUpdateString(jsonObj_1,"document_sub_type","passport")
    CkJsonObject::ckUpdateString(jsonObj_1,"content","QWxwYWNhcyBjYW5ub3QgbGl2ZSBhbG9uZS4=")
    CkJsonObject::ckUpdateString(jsonObj_1,"mime_type","image/jpeg")
    CkJsonObject::ckDispose(jsonObj_1)

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckEmitSb(jarr,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"POST","https://domain.com/v1/accounts/:account_id/documents/upload",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonArray::ckDispose(jarr)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonArray::ckDispose(jarr)
    CkStringBuilder::ckDispose(sbRequestBody)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-u '{{api_key}}:{{api_secret}}'
	-d '[
  {
    "document_type": "cip_result",
    "content": "VGhlcmUgYXJlIG5vIHdpbGQgYWxwYWNhcy4=",
    "mime_type": "application/pdf"
  },
  {
    "document_type": "identity_verification",
    "document_sub_type": "passport",
    "content": "QWxwYWNhcyBjYW5ub3QgbGl2ZSBhbG9uZS4=",
    "mime_type": "image/jpeg"
  }
]'
https://domain.com/v1/accounts/:account_id/documents/upload

Postman Collection Item JSON

{
  "name": "Document",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n  {\n    \"document_type\": \"cip_result\",\n    \"content\": \"VGhlcmUgYXJlIG5vIHdpbGQgYWxwYWNhcy4=\",\n    \"mime_type\": \"application/pdf\"\n  },\n  {\n    \"document_type\": \"identity_verification\",\n    \"document_sub_type\": \"passport\",\n    \"content\": \"QWxwYWNhcyBjYW5ub3QgbGl2ZSBhbG9uZS4=\",\n    \"mime_type\": \"image/jpeg\"\n  }\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{HOST}}/v1/accounts/:account_id/documents/upload",
      "host": [
        "{{HOST}}"
      ],
      "path": [
        "v1",
        "accounts",
        ":account_id",
        "documents",
        "upload"
      ],
      "variable": [
        {
          "key": "account_id",
          "value": null
        }
      ]
    },
    "description": "| Key               | Value                 | Requirement |\n|-------------------|-----------------------|:-----------:|\n| `document_upload` | models.DocumentUpload | Required    |"
  },
  "response": [
  ]
}