Chilkat Online Tools

Swift3 / Coupa Postman Collection - OAuth - Master / Add contract term

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 XML: Generate Code to Create XML

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

    // <?xml version="1.0" encoding="UTF-8"?>
    // <contract-term>
    //     <name>API Contract Term 1</name>
    //     <contract-term-type>PriceRangeContractTerm</contract-term-type>
    //     <use-pct-discounts type="boolean">true</use-pct-discounts>
    //     <tier-1-upper-bound type="decimal">15.0</tier-1-upper-bound>
    //     <tier-1-disc-pct type="decimal">6.0</tier-1-disc-pct>
    //     <tier-2-disc-pct type="decimal">7.0</tier-2-disc-pct>
    // </contract-term>
    // 

    let xml = CkoXml()!
    xml.tag = "contract-term"
    xml.updateChildContent("name", value: "API Contract Term 1")
    xml.updateChildContent("contract-term-type", value: "PriceRangeContractTerm")
    xml.updateAttr(at: "use-pct-discounts", autoCreate: true, attrName: "type", attrValue: "boolean")
    xml.updateChildContent("use-pct-discounts", value: "true")
    xml.updateAttr(at: "tier-1-upper-bound", autoCreate: true, attrName: "type", attrValue: "decimal")
    xml.updateChildContent("tier-1-upper-bound", value: "15.0")
    xml.updateAttr(at: "tier-1-disc-pct", autoCreate: true, attrName: "type", attrValue: "decimal")
    xml.updateChildContent("tier-1-disc-pct", value: "6.0")
    xml.updateAttr(at: "tier-2-disc-pct", autoCreate: true, attrName: "type", attrValue: "decimal")
    xml.updateChildContent("tier-2-disc-pct", value: "7.0")

    // Adds the "Authorization: Bearer <access_token>" header.
    http.authToken = "<access_token>"

    let sbRequestBody = CkoStringBuilder()!
    xml.getSb(sbRequestBody)

    var resp: CkoHttpResponse? = http.pTextSb("POST", url: "https://domain.com/contracts/:id/contract_terms", textData: sbRequestBody, charset: "utf-8", contentType: "application/xml", 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>"
	-d '<?xml version="1.0" encoding="UTF-8"?>
<contract-term>
    <name>API Contract Term 1</name>
    <contract-term-type>PriceRangeContractTerm</contract-term-type>
    <use-pct-discounts type="boolean">true</use-pct-discounts>
    <tier-1-upper-bound type="decimal">15.0</tier-1-upper-bound>
    <tier-1-disc-pct type="decimal">6.0</tier-1-disc-pct>
    <tier-2-disc-pct type="decimal">7.0</tier-2-disc-pct>
</contract-term>'
https://domain.com/contracts/:id/contract_terms

Postman Collection Item JSON

{
  "name": "Add contract term",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<contract-term>\n    <name>API Contract Term 1</name>\n    <contract-term-type>PriceRangeContractTerm</contract-term-type>\n    <use-pct-discounts type=\"boolean\">true</use-pct-discounts>\n    <tier-1-upper-bound type=\"decimal\">15.0</tier-1-upper-bound>\n    <tier-1-disc-pct type=\"decimal\">6.0</tier-1-disc-pct>\n    <tier-2-disc-pct type=\"decimal\">7.0</tier-2-disc-pct>\n</contract-term>",
      "options": {
        "raw": {
          "language": "xml"
        }
      }
    },
    "url": {
      "raw": "{{URL}}/contracts/:id/contract_terms",
      "host": [
        "{{URL}}"
      ],
      "path": [
        "contracts",
        ":id",
        "contract_terms"
      ],
      "variable": [
        {
          "key": "id",
          "value": "377",
          "type": "string"
        }
      ]
    }
  },
  "response": [
  ]
}