Chilkat Online Tools

PureBasic / SimpleKart E-commerce API / Post order details to the database

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.

    ; {
    ;   "address": "<string>",
    ;   "item": {
    ;     "itemId": "<integer>",
    ;     "name": "<string>",
    ;     "price": "<integer>",
    ;     "description": "<string>"
    ;   }
    ; }

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

    CkJsonObject::ckUpdateString(json,"address","<string>")
    CkJsonObject::ckUpdateString(json,"item.itemId","<integer>")
    CkJsonObject::ckUpdateString(json,"item.name","<string>")
    CkJsonObject::ckUpdateString(json,"item.price","<integer>")
    CkJsonObject::ckUpdateString(json,"item.description","<string>")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://api.simplekart.com/orders","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)

    ; {
    ;   "orderId": 674,
    ;   "items": [
    ;     {
    ;       "itemId": 1657,
    ;       "name": "Smart WiFi Router",
    ;       "price": 2096,
    ;       "description": "Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH"
    ;     }
    ;   ],
    ;   "arrivalDate": "2020-3-04T13:07:45.962Z",
    ;   "orderDate": "2020-03-06T19:27:26.733Z",
    ;   "orderTotal": 2096,
    ;   "address": "95 Tarkiln Hill St.Laurel, MD 20707"
    ; }

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

    itemId.i
    name.s
    price.i
    description.s

    orderId.i = CkJsonObject::ckIntOf(jResp,"orderId")
    arrivalDate.s = CkJsonObject::ckStringOf(jResp,"arrivalDate")
    orderDate.s = CkJsonObject::ckStringOf(jResp,"orderDate")
    orderTotal.i = CkJsonObject::ckIntOf(jResp,"orderTotal")
    address.s = CkJsonObject::ckStringOf(jResp,"address")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"items")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        itemId = CkJsonObject::ckIntOf(jResp,"items[i].itemId")
        name = CkJsonObject::ckStringOf(jResp,"items[i].name")
        price = CkJsonObject::ckIntOf(jResp,"items[i].price")
        description = CkJsonObject::ckStringOf(jResp,"items[i].description")
        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-d '{
    "address": "<string>",
    "item": {
        "itemId": "<integer>",
        "name": "<string>",
        "price": "<integer>",
        "description": "<string>"
    }
}'
https://api.simplekart.com/orders

Postman Collection Item JSON

{
  "name": "Post order details to the database",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"address\": \"<string>\",\n    \"item\": {\n        \"itemId\": \"<integer>\",\n        \"name\": \"<string>\",\n        \"price\": \"<integer>\",\n        \"description\": \"<string>\"\n    }\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/orders",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "orders"
      ]
    },
    "description": "Post item order to orders database"
  },
  "response": [
    {
      "name": "OK - Order created",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"items\": [\n        {\n            \"itemId\": 1657,\n            \"name\": \"Smart WiFi Router\",\n            \"price\": 2096,\n            \"description\": \"Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH\"\n        }\n    ],\n    \"address\": \"95 Tarkiln Hill St.Laurel, MD 20707\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/orders",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "orders"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"orderId\": 674,\n \"items\": [\n  {\n   \"itemId\": 1657,\n   \"name\": \"Smart WiFi Router\",\n   \"price\": 2096,\n   \"description\": \"Wifi router/wireless router. One of routers for wireless internet, wireless routers for home. Dual band router and gigabit router. Ideal as internet router also gaming router. Long range coverage with high speed. Compatible with all 802.11ac devices and below. 5ghz router/5g router Frequency Range: 2.4GHz and 5GHz; Interface Available: 4 x 10/100/1000Mbps LAN Ports, 1 10/100/1000Mbps WAN Port, 1 USB 2.0 Ports; Protocols Supported: Supports IPv4 and IPv6; System Requirement: Microsoft Windows 98SE NT 2000 XP Vista, or Windows 7 Windows 8/8.1/10, MAC OS NetWare UNIX or Linux; Wireless Standards: IEEE 802.11ac/n/a 5GHz, IEEE 802.11b/g/n 2.4GH\"\n  }\n ],\n \"arrivalDate\": \"2020-3-04T13:07:45.962Z\",\n \"orderDate\": \"2020-03-06T19:27:26.733Z\",\n \"orderTotal\": 2096,\n \"address\": \"95 Tarkiln Hill St.Laurel, MD 20707\"\n}"
    }
  ]
}