Chilkat Online Tools

PureBasic / easybill REST API / Create WebHook

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.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

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

    ; The following JSON is sent in the request body.

    ; {
    ;   "url": "<string>",
    ;   "content_type": "json",
    ;   "secret": "<string>",
    ;   "events": [
    ;     "position.update",
    ;     "document.create"
    ;   ],
    ;   "description": "<string>",
    ;   "id": "<long>",
    ;   "is_active": false,
    ;   "last_response": {
    ;     "date": "<dateTime>",
    ;     "code": "<integer>",
    ;     "response": "<string>"
    ;   }
    ; }

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

    CkJsonObject::ckUpdateString(json,"url","<string>")
    CkJsonObject::ckUpdateString(json,"content_type","json")
    CkJsonObject::ckUpdateString(json,"secret","<string>")
    CkJsonObject::ckUpdateString(json,"events[0]","position.update")
    CkJsonObject::ckUpdateString(json,"events[1]","document.create")
    CkJsonObject::ckUpdateString(json,"description","<string>")
    CkJsonObject::ckUpdateString(json,"id","<long>")
    CkJsonObject::ckUpdateBool(json,"is_active",0)
    CkJsonObject::ckUpdateString(json,"last_response.date","<dateTime>")
    CkJsonObject::ckUpdateString(json,"last_response.code","<integer>")
    CkJsonObject::ckUpdateString(json,"last_response.response","<string>")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")
    CkHttp::ckSetRequestHeader(http,"Authorization","{{apiKey}}")
    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://api.easybill.de/rest/v1/webhooks","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

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

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

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

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

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

    ; {
    ;   "url": "<string>",
    ;   "content_type": "json",
    ;   "secret": "<string>",
    ;   "events": [
    ;     "position.update",
    ;     "document.create"
    ;   ],
    ;   "description": "<string>",
    ;   "id": "<long>",
    ;   "is_active": false,
    ;   "last_response": {
    ;     "date": "<dateTime>",
    ;     "code": "<integer>",
    ;     "response": "<string>"
    ;   }
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    strVal.s

    url.s = CkJsonObject::ckStringOf(jResp,"url")
    content_type.s = CkJsonObject::ckStringOf(jResp,"content_type")
    secret.s = CkJsonObject::ckStringOf(jResp,"secret")
    description.s = CkJsonObject::ckStringOf(jResp,"description")
    id.s = CkJsonObject::ckStringOf(jResp,"id")
    is_active.i = CkJsonObject::ckBoolOf(jResp,"is_active")
    Date.s = CkJsonObject::ckStringOf(jResp,"last_response.date")
    Code.s = CkJsonObject::ckStringOf(jResp,"last_response.code")
    Response.s = CkJsonObject::ckStringOf(jResp,"last_response.response")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"events")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        strVal = CkJsonObject::ckStringOf(jResp,"events[i]")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Authorization: {{apiKey}}"
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "url": "<string>",
  "content_type": "json",
  "secret": "<string>",
  "events": [
    "position.update",
    "document.create"
  ],
  "description": "<string>",
  "id": "<long>",
  "is_active": false,
  "last_response": {
    "date": "<dateTime>",
    "code": "<integer>",
    "response": "<string>"
  }
}'
https://api.easybill.de/rest/v1/webhooks

Postman Collection Item JSON

{
  "name": "Create WebHook",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"url\": \"<string>\",\n  \"content_type\": \"json\",\n  \"secret\": \"<string>\",\n  \"events\": [\n    \"position.update\",\n    \"document.create\"\n  ],\n  \"description\": \"<string>\",\n  \"id\": \"<long>\",\n  \"is_active\": false,\n  \"last_response\": {\n    \"date\": \"<dateTime>\",\n    \"code\": \"<integer>\",\n    \"response\": \"<string>\"\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/webhooks",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "webhooks"
      ]
    }
  },
  "response": [
    {
      "name": "Successful operation",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "Authorization",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"url\": \"<string>\",\n  \"content_type\": \"json\",\n  \"secret\": \"<string>\",\n  \"events\": [\n    \"position.update\",\n    \"document.create\"\n  ],\n  \"description\": \"<string>\",\n  \"id\": \"<long>\",\n  \"is_active\": false,\n  \"last_response\": {\n    \"date\": \"<dateTime>\",\n    \"code\": \"<integer>\",\n    \"response\": \"<string>\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "webhooks"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"url\": \"<string>\",\n  \"content_type\": \"json\",\n  \"secret\": \"<string>\",\n  \"events\": [\n    \"position.update\",\n    \"document.create\"\n  ],\n  \"description\": \"<string>\",\n  \"id\": \"<long>\",\n  \"is_active\": false,\n  \"last_response\": {\n    \"date\": \"<dateTime>\",\n    \"code\": \"<integer>\",\n    \"response\": \"<string>\"\n  }\n}"
    },
    {
      "name": "Too Many Requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "Authorization",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"url\": \"<string>\",\n  \"content_type\": \"json\",\n  \"secret\": \"<string>\",\n  \"events\": [\n    \"position.update\",\n    \"document.create\"\n  ],\n  \"description\": \"<string>\",\n  \"id\": \"<long>\",\n  \"is_active\": false,\n  \"last_response\": {\n    \"date\": \"<dateTime>\",\n    \"code\": \"<integer>\",\n    \"response\": \"<string>\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "webhooks"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "text",
      "header": [
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}