Chilkat Online Tools

Swift3 / Coupa Postman Collection - OAuth - Master / Create a Requisition in Draft Status

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"?>
    // <requisition-header>
    //     <justification>Test</justification>
    //     <ship-to-attention>Coupa Buyer</ship-to-attention>
    //     <currency>
    //         <code>USD</code>
    //     </currency>
    //     <requested-by>
    //         <login>CoupaSam</login>
    //         <email>CoupaSam@coupa.com</email>
    //     </requested-by>
    //     <ship-to-address>
    //         <location-code>DUMMYST</location-code>
    //     </ship-to-address>
    //     <requisition-lines type="array">
    //         <requisition-line>
    //             <description>Test</description>
    //             <line-num type="integer">1</line-num>
    //             <total type="decimal">10.00</total>
    //             <line-type>RequisitionAmountLine</line-type>
    //             <unit-price type="decimal">10.00</unit-price>
    //             <need-by-date type="dateTime">2020-05-15T00:00:00-05:00</need-by-date>
    //             <account>
    //                 <code>DUM_1_SEG_1_LV_1-DUM_1_SEG_2_LV_1</code>
    //                 <account-type>
    //                     <name>Dummy Chart of Accounts</name>
    //                     <currency>
    //                         <code>USD</code>
    //                     </currency>
    //                 </account-type>
    //             </account>
    //             <currency>
    //                 <code>USD</code>
    //             </currency>
    //             <supplier>
    //                 <name>CoupaTester</name>
    //                 <number>123456</number>
    //             </supplier>
    //         </requisition-line>
    //     </requisition-lines>
    // </requisition-header>
    // 

    let xml = CkoXml()!
    xml.tag = "requisition-header"
    xml.updateChildContent("justification", value: "Test")
    xml.updateChildContent("ship-to-attention", value: "Coupa Buyer")
    xml.updateChildContent("currency|code", value: "USD")
    xml.updateChildContent("requested-by|login", value: "CoupaSam")
    xml.updateChildContent("requested-by|email", value: "CoupaSam@coupa.com")
    xml.updateChildContent("ship-to-address|location-code", value: "DUMMYST")
    xml.updateAttr(at: "requisition-lines", autoCreate: true, attrName: "type", attrValue: "array")
    xml.updateChildContent("requisition-lines|requisition-line|description", value: "Test")
    xml.updateAttr(at: "requisition-lines|requisition-line|line-num", autoCreate: true, attrName: "type", attrValue: "integer")
    xml.updateChildContentInt("requisition-lines|requisition-line|line-num", value: 1)
    xml.updateAttr(at: "requisition-lines|requisition-line|total", autoCreate: true, attrName: "type", attrValue: "decimal")
    xml.updateChildContent("requisition-lines|requisition-line|total", value: "10.00")
    xml.updateChildContent("requisition-lines|requisition-line|line-type", value: "RequisitionAmountLine")
    xml.updateAttr(at: "requisition-lines|requisition-line|unit-price", autoCreate: true, attrName: "type", attrValue: "decimal")
    xml.updateChildContent("requisition-lines|requisition-line|unit-price", value: "10.00")
    xml.updateAttr(at: "requisition-lines|requisition-line|need-by-date", autoCreate: true, attrName: "type", attrValue: "dateTime")
    xml.updateChildContent("requisition-lines|requisition-line|need-by-date", value: "2020-05-15T00:00:00-05:00")
    xml.updateChildContent("requisition-lines|requisition-line|account|code", value: "DUM_1_SEG_1_LV_1-DUM_1_SEG_2_LV_1")
    xml.updateChildContent("requisition-lines|requisition-line|account|account-type|name", value: "Dummy Chart of Accounts")
    xml.updateChildContent("requisition-lines|requisition-line|account|account-type|currency|code", value: "USD")
    xml.updateChildContent("requisition-lines|requisition-line|currency|code", value: "USD")
    xml.updateChildContent("requisition-lines|requisition-line|supplier|name", value: "CoupaTester")
    xml.updateChildContentInt("requisition-lines|requisition-line|supplier|number", value: 123456)

    // 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/requisitions/?fields=[\"id\"]", 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"?>
<requisition-header>
    <justification>Test</justification>
    <ship-to-attention>Coupa Buyer</ship-to-attention>
    <currency>
        <code>USD</code>
    </currency>
    <requested-by>
        <login>CoupaSam</login>
        <email>CoupaSam@coupa.com</email>
    </requested-by>
    <ship-to-address>
        <location-code>DUMMYST</location-code>
    </ship-to-address>
    <requisition-lines type="array">
        <requisition-line>
            <description>Test</description>
            <line-num type="integer">1</line-num>
            <total type="decimal">10.00</total>
            <line-type>RequisitionAmountLine</line-type>
            <unit-price type="decimal">10.00</unit-price>
            <need-by-date type="dateTime">2020-05-15T00:00:00-05:00</need-by-date>
            <account>
                <code>DUM_1_SEG_1_LV_1-DUM_1_SEG_2_LV_1</code>
                <account-type>
                    <name>Dummy Chart of Accounts</name>
                    <currency>
                        <code>USD</code>
                    </currency>
                </account-type>
            </account>
            <currency>
                <code>USD</code>
            </currency>
            <supplier>
                <name>CoupaTester</name>
                <number>123456</number>
            </supplier>
        </requisition-line>
    </requisition-lines>
</requisition-header>'
https://domain.com/requisitions/?fields=["id"]

Postman Collection Item JSON

{
  "name": "Create a Requisition in Draft Status",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<requisition-header>\n    <justification>Test</justification>\n    <ship-to-attention>Coupa Buyer</ship-to-attention>\n    <currency>\n        <code>USD</code>\n    </currency>\n    <requested-by>\n        <login>CoupaSam</login>\n        <email>CoupaSam@coupa.com</email>\n    </requested-by>\n    <ship-to-address>\n        <location-code>DUMMYST</location-code>\n    </ship-to-address>\n    <requisition-lines type=\"array\">\n        <requisition-line>\n            <description>Test</description>\n            <line-num type=\"integer\">1</line-num>\n            <total type=\"decimal\">10.00</total>\n            <line-type>RequisitionAmountLine</line-type>\n            <unit-price type=\"decimal\">10.00</unit-price>\n            <need-by-date type=\"dateTime\">2020-05-15T00:00:00-05:00</need-by-date>\n            <account>\n                <code>DUM_1_SEG_1_LV_1-DUM_1_SEG_2_LV_1</code>\n                <account-type>\n                    <name>Dummy Chart of Accounts</name>\n                    <currency>\n                        <code>USD</code>\n                    </currency>\n                </account-type>\n            </account>\n            <currency>\n                <code>USD</code>\n            </currency>\n            <supplier>\n                <name>CoupaTester</name>\n                <number>123456</number>\n            </supplier>\n        </requisition-line>\n    </requisition-lines>\n</requisition-header>",
      "options": {
        "raw": {
          "language": "xml"
        }
      }
    },
    "url": {
      "raw": "{{URL}}/requisitions/?fields=[\"id\"]",
      "host": [
        "{{URL}}"
      ],
      "path": [
        "requisitions",
        ""
      ],
      "query": [
        {
          "key": "fields",
          "value": "[\"id\"]"
        }
      ]
    }
  },
  "response": [
  ]
}