Chilkat Online Tools

Swift / Commercetools / Create DiscountCode

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.

    // {
    //   "code": "SAVE10",
    //   "name": {
    //     "en": "Save10"
    //   },
    //   "cartDiscounts": [
    //     {
    //       "typeId": "cart-discount",
    //       "id": "fdbaf4ea-fbc9-4fea-bac4-1d7e6c1995b3"
    //     }
    //   ],
    //   "isActive": true,
    //   "cartPredicate": "1=1"
    // }

    let json = CkoJsonObject()
    json.UpdateString("code", value: "SAVE10")
    json.UpdateString("name.en", value: "Save10")
    json.UpdateString("cartDiscounts[0].typeId", value: "cart-discount")
    json.UpdateString("cartDiscounts[0].id", value: "fdbaf4ea-fbc9-4fea-bac4-1d7e6c1995b3")
    json.UpdateBool("isActive", value: true)
    json.UpdateString("cartPredicate", value: "1=1")

    // Adds the "Authorization: Bearer <access_token>" header.
    http.AuthToken = "<access_token>"
    http.SetRequestHeader("Content-Type", value: "application/json")

    let sbRequestBody = CkoStringBuilder()
    json.EmitSb(sbRequestBody)

    var resp: CkoHttpResponse? = http.PTextSb("post", url: "https://domain.com/{{project-key}}/discount-codes", textData: sbRequestBody, charset: "utf-8", contentType: "application/json", md5: false, gzip: false)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    print("\(resp!.StatusCode.intValue)")
    print("\(resp!.BodyStr)")
    resp = nil

}

Curl Command

curl -X post
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "code" : "SAVE10",
  "name" : {
    "en" : "Save10"
  },
  "cartDiscounts" : [ {
    "typeId" : "cart-discount",
    "id" : "fdbaf4ea-fbc9-4fea-bac4-1d7e6c1995b3"
  } ],
  "isActive" : true,
  "cartPredicate" : "1=1"
}'
https://domain.com/{{project-key}}/discount-codes

Postman Collection Item JSON

{
  "name": "Create DiscountCode",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "tests[\"Status code \" + responseCode.code] = responseCode.code === 200 || responseCode.code === 201;",
          "var data = JSON.parse(responseBody);",
          "if(data.results && data.results[0] && data.results[0].id && data.results[0].version){",
          "    pm.environment.set(\"discount-code-id\", data.results[0].id); ",
          "    pm.environment.set(\"discount-code-version\", data.results[0].version);",
          "}",
          "if(data.results && data.results[0] && data.results[0].key){",
          "    pm.environment.set(\"discount-code-key\", data.results[0].key); ",
          "}",
          "if(data.version){",
          "    pm.environment.set(\"discount-code-version\", data.version);",
          "}",
          "if(data.id){",
          "    pm.environment.set(\"discount-code-id\", data.id); ",
          "}",
          "if(data.key){",
          "    pm.environment.set(\"discount-code-key\", data.key);",
          "}",
          ""
        ]
      }
    }
  ],
  "request": {
    "auth": {
      "type": "oauth2",
      "oauth2": {
        "accessToken": "{{ctp_access_token}}",
        "addTokenTo": "header",
        "tokenType": "Bearer"
      }
    },
    "method": "post",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "url": {
      "raw": "{{host}}/{{project-key}}/discount-codes",
      "host": [
        "{{host}}"
      ],
      "path": [
        "{{project-key}}",
        "discount-codes"
      ],
      "query": [
        {
          "key": "expand",
          "value": "",
          "equals": true,
          "disabled": true
        }
      ]
    },
    "description": "null",
    "body": {
      "mode": "raw",
      "raw": "{\n  \"code\" : \"SAVE10\",\n  \"name\" : {\n    \"en\" : \"Save10\"\n  },\n  \"cartDiscounts\" : [ {\n    \"typeId\" : \"cart-discount\",\n    \"id\" : \"fdbaf4ea-fbc9-4fea-bac4-1d7e6c1995b3\"\n  } ],\n  \"isActive\" : true,\n  \"cartPredicate\" : \"1=1\"\n}"
    }
  },
  "response": [
  ]
}