Chilkat Online Tools

Swift3 / easybill REST API / Create new contact

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!
    var success: Bool

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

    // The following JSON is sent in the request body.

    // {
    //   "city": "<string>",
    //   "street": "<string>",
    //   "state": "<string>",
    //   "company_name": "<string>",
    //   "country": "<string>",
    //   "department": null,
    //   "fax": null,
    //   "first_name": null,
    //   "id": "<long>",
    //   "last_name": null,
    //   "login_id": "<long>",
    //   "mobile": null,
    //   "note": null,
    //   "personal": false,
    //   "phone_1": null,
    //   "phone_2": null,
    //   "salutation": "<integer>",
    //   "suffix_1": null,
    //   "suffix_2": null,
    //   "title": null,
    //   "zip_code": null,
    //   "created_at": "<string>",
    //   "updated_at": "<string>"
    // }

    let json = CkoJsonObject()!
    json.update("city", value: "<string>")
    json.update("street", value: "<string>")
    json.update("state", value: "<string>")
    json.update("company_name", value: "<string>")
    json.update("country", value: "<string>")
    json.updateNull("department")
    json.updateNull("fax")
    json.updateNull("first_name")
    json.update("id", value: "<long>")
    json.updateNull("last_name")
    json.update("login_id", value: "<long>")
    json.updateNull("mobile")
    json.updateNull("note")
    json.updateBool("personal", value: false)
    json.updateNull("phone_1")
    json.updateNull("phone_2")
    json.update("salutation", value: "<integer>")
    json.updateNull("suffix_1")
    json.updateNull("suffix_2")
    json.updateNull("title")
    json.updateNull("zip_code")
    json.update("created_at", value: "<string>")
    json.update("updated_at", value: "<string>")

    http.setRequestHeader("Content-Type", value: "application/json")
    http.setRequestHeader("Authorization", value: "{{apiKey}}")
    http.setRequestHeader("Accept", value: "application/json")

    var resp: CkoHttpResponse? = http.postJson3("https://api.easybill.de/rest/v1/customers/:customerId/contacts", contentType: "application/json", json: json)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp!.getBodySb(sbResponseBody)

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp!.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.header!)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

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

    // {
    //   "city": "<string>",
    //   "street": "<string>",
    //   "state": "<string>",
    //   "company_name": "<string>",
    //   "country": "<string>",
    //   "department": null,
    //   "fax": null,
    //   "first_name": null,
    //   "id": "<long>",
    //   "last_name": null,
    //   "login_id": "<long>",
    //   "mobile": null,
    //   "note": null,
    //   "personal": false,
    //   "phone_1": null,
    //   "phone_2": null,
    //   "salutation": "<integer>",
    //   "suffix_1": null,
    //   "suffix_2": null,
    //   "title": null,
    //   "zip_code": null,
    //   "created_at": "<string>",
    //   "updated_at": "<string>"
    // }

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

    var city: String? = jResp.string(of: "city")
    var street: String? = jResp.string(of: "street")
    var state: String? = jResp.string(of: "state")
    var company_name: String? = jResp.string(of: "company_name")
    var country: String? = jResp.string(of: "country")
    var department: String? = jResp.string(of: "department")
    var fax: String? = jResp.string(of: "fax")
    var first_name: String? = jResp.string(of: "first_name")
    var id: String? = jResp.string(of: "id")
    var last_name: String? = jResp.string(of: "last_name")
    var login_id: String? = jResp.string(of: "login_id")
    var mobile: String? = jResp.string(of: "mobile")
    var note: String? = jResp.string(of: "note")
    var personal: Bool = jResp.bool(of: "personal")
    var phone_1: String? = jResp.string(of: "phone_1")
    var phone_2: String? = jResp.string(of: "phone_2")
    var salutation: String? = jResp.string(of: "salutation")
    var suffix_1: String? = jResp.string(of: "suffix_1")
    var suffix_2: String? = jResp.string(of: "suffix_2")
    var title: String? = jResp.string(of: "title")
    var zip_code: String? = jResp.string(of: "zip_code")
    var created_at: String? = jResp.string(of: "created_at")
    var updated_at: String? = jResp.string(of: "updated_at")

}

Curl Command

curl -X POST
	-H "Authorization: {{apiKey}}"
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "city": "<string>",
  "street": "<string>",
  "state": "<string>",
  "company_name": "<string>",
  "country": "<string>",
  "department": null,
  "fax": null,
  "first_name": null,
  "id": "<long>",
  "last_name": null,
  "login_id": "<long>",
  "mobile": null,
  "note": null,
  "personal": false,
  "phone_1": null,
  "phone_2": null,
  "salutation": "<integer>",
  "suffix_1": null,
  "suffix_2": null,
  "title": null,
  "zip_code": null,
  "created_at": "<string>",
  "updated_at": "<string>"
}'
https://api.easybill.de/rest/v1/customers/:customerId/contacts

Postman Collection Item JSON

{
  "name": "Create new contact",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"city\": \"<string>\",\n  \"street\": \"<string>\",\n  \"state\": \"<string>\",\n  \"company_name\": \"<string>\",\n  \"country\": \"<string>\",\n  \"department\": null,\n  \"fax\": null,\n  \"first_name\": null,\n  \"id\": \"<long>\",\n  \"last_name\": null,\n  \"login_id\": \"<long>\",\n  \"mobile\": null,\n  \"note\": null,\n  \"personal\": false,\n  \"phone_1\": null,\n  \"phone_2\": null,\n  \"salutation\": \"<integer>\",\n  \"suffix_1\": null,\n  \"suffix_2\": null,\n  \"title\": null,\n  \"zip_code\": null,\n  \"created_at\": \"<string>\",\n  \"updated_at\": \"<string>\"\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/customers/:customerId/contacts",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "customers",
        ":customerId",
        "contacts"
      ],
      "variable": [
        {
          "key": "customerId",
          "value": "<long>",
          "description": "(Required) ID of customer"
        }
      ]
    }
  },
  "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  \"city\": \"<string>\",\n  \"street\": \"<string>\",\n  \"state\": \"<string>\",\n  \"company_name\": \"<string>\",\n  \"country\": \"<string>\",\n  \"department\": null,\n  \"fax\": null,\n  \"first_name\": null,\n  \"id\": \"<long>\",\n  \"last_name\": null,\n  \"login_id\": \"<long>\",\n  \"mobile\": null,\n  \"note\": null,\n  \"personal\": false,\n  \"phone_1\": null,\n  \"phone_2\": null,\n  \"salutation\": \"<integer>\",\n  \"suffix_1\": null,\n  \"suffix_2\": null,\n  \"title\": null,\n  \"zip_code\": null,\n  \"created_at\": \"<string>\",\n  \"updated_at\": \"<string>\"\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/customers/:customerId/contacts",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "customers",
            ":customerId",
            "contacts"
          ],
          "variable": [
            {
              "key": "customerId"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"city\": \"<string>\",\n  \"street\": \"<string>\",\n  \"state\": \"<string>\",\n  \"company_name\": \"<string>\",\n  \"country\": \"<string>\",\n  \"department\": null,\n  \"fax\": null,\n  \"first_name\": null,\n  \"id\": \"<long>\",\n  \"last_name\": null,\n  \"login_id\": \"<long>\",\n  \"mobile\": null,\n  \"note\": null,\n  \"personal\": false,\n  \"phone_1\": null,\n  \"phone_2\": null,\n  \"salutation\": \"<integer>\",\n  \"suffix_1\": null,\n  \"suffix_2\": null,\n  \"title\": null,\n  \"zip_code\": null,\n  \"created_at\": \"<string>\",\n  \"updated_at\": \"<string>\"\n}"
    },
    {
      "name": "Invalid contact",
      "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  \"city\": \"<string>\",\n  \"street\": \"<string>\",\n  \"state\": \"<string>\",\n  \"company_name\": \"<string>\",\n  \"country\": \"<string>\",\n  \"department\": null,\n  \"fax\": null,\n  \"first_name\": null,\n  \"id\": \"<long>\",\n  \"last_name\": null,\n  \"login_id\": \"<long>\",\n  \"mobile\": null,\n  \"note\": null,\n  \"personal\": false,\n  \"phone_1\": null,\n  \"phone_2\": null,\n  \"salutation\": \"<integer>\",\n  \"suffix_1\": null,\n  \"suffix_2\": null,\n  \"title\": null,\n  \"zip_code\": null,\n  \"created_at\": \"<string>\",\n  \"updated_at\": \"<string>\"\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/customers/:customerId/contacts",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "customers",
            ":customerId",
            "contacts"
          ],
          "variable": [
            {
              "key": "customerId"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "text",
      "header": [
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "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  \"city\": \"<string>\",\n  \"street\": \"<string>\",\n  \"state\": \"<string>\",\n  \"company_name\": \"<string>\",\n  \"country\": \"<string>\",\n  \"department\": null,\n  \"fax\": null,\n  \"first_name\": null,\n  \"id\": \"<long>\",\n  \"last_name\": null,\n  \"login_id\": \"<long>\",\n  \"mobile\": null,\n  \"note\": null,\n  \"personal\": false,\n  \"phone_1\": null,\n  \"phone_2\": null,\n  \"salutation\": \"<integer>\",\n  \"suffix_1\": null,\n  \"suffix_2\": null,\n  \"title\": null,\n  \"zip_code\": null,\n  \"created_at\": \"<string>\",\n  \"updated_at\": \"<string>\"\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/customers/:customerId/contacts",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "customers",
            ":customerId",
            "contacts"
          ],
          "variable": [
            {
              "key": "customerId"
            }
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "text",
      "header": [
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}