Chilkat Online Tools

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

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Json
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
integer li_ItemId
string ls_Name
integer li_Price
string ls_Description
integer li_OrderId
string ls_ArrivalDate
string ls_OrderDate
integer li_OrderTotal
string ls_Address
integer i
integer li_Count_i

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.UpdateString("address","<string>")
loo_Json.UpdateString("item.itemId","<integer>")
loo_Json.UpdateString("item.name","<string>")
loo_Json.UpdateString("item.price","<integer>")
loo_Json.UpdateString("item.description","<string>")

loo_Http.SetRequestHeader("Content-Type","application/json")

loo_Resp = loo_Http.PostJson3("https://api.simplekart.com/orders","application/json",loo_Json)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_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

li_OrderId = loo_JResp.IntOf("orderId")
ls_ArrivalDate = loo_JResp.StringOf("arrivalDate")
ls_OrderDate = loo_JResp.StringOf("orderDate")
li_OrderTotal = loo_JResp.IntOf("orderTotal")
ls_Address = loo_JResp.StringOf("address")
i = 0
li_Count_i = loo_JResp.SizeOfArray("items")
do while i < li_Count_i
    loo_JResp.I = i
    li_ItemId = loo_JResp.IntOf("items[i].itemId")
    ls_Name = loo_JResp.StringOf("items[i].name")
    li_Price = loo_JResp.IntOf("items[i].price")
    ls_Description = loo_JResp.StringOf("items[i].description")
    i = i + 1
loop


destroy loo_Http
destroy loo_Json
destroy loo_SbResponseBody
destroy loo_JResp

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