Chilkat Online Tools

PureBasic / Coupa Postman Collection - OAuth - Master / Update Existing Department

Back to Collection Items

IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkXml.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 XML: Generate Code to Create XML

    ; The following XML is sent in the request body.

    ; <?xml version="1.0" encoding="utf-8"?>
    ; <department>
    ;     <name>Coupa Development</name>
    ;     <active>false</active>
    ;     <custom-fields>
    ;         <test1>TestCustomDevelopmenting</test1>
    ;     </custom-fields>
    ; </department>
    ; 

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

    CkXml::setCkTag(xml, "department")
    CkXml::ckUpdateChildContent(xml,"name","Coupa Development")
    CkXml::ckUpdateChildContent(xml,"active","false")
    CkXml::ckUpdateChildContent(xml,"custom-fields|test1","TestCustomDevelopmenting")

    ; Adds the "Authorization: Bearer <access_token>" header.
    CkHttp::setCkAuthToken(http, "<access_token>")

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

    CkXml::ckGetXmlSb(xml,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PUT","https://domain.com/departments/:id?fields=[" + Chr(34) + "id" + Chr(34) + "," + Chr(34) + "name" + Chr(34) + "," + Chr(34) + "active" + Chr(34) + ",{" + Chr(34) + "custom_fields" + Chr(34) + ":{}}]",sbRequestBody,"utf-8","application/xml",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbRequestBody)


    ProcedureReturn
EndProcedure

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-d '<department>
    <name>Coupa Development</name>
    <active>false</active>
    <custom-fields>
        <test1>TestCustomDevelopmenting</test1>
    </custom-fields>
</department>'
https://domain.com/departments/:id?fields=["id","name","active",{"custom_fields":{}}]

Postman Collection Item JSON

{
  "name": "Update Existing Department",
  "request": {
    "method": "PUT",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "<department>\n    <name>Coupa Development</name>\n    <active>false</active>\n    <custom-fields>\n        <test1>TestCustomDevelopmenting</test1>\n    </custom-fields>\n</department>"
    },
    "url": {
      "raw": "{{URL}}/departments/:id?fields=[\"id\",\"name\",\"active\",{\"custom_fields\":{}}]",
      "host": [
        "{{URL}}"
      ],
      "path": [
        "departments",
        ":id"
      ],
      "query": [
        {
          "key": "fields",
          "value": "[\"id\",\"name\",\"active\",{\"custom_fields\":{}}]"
        }
      ],
      "variable": [
        {
          "key": "id",
          "value": "151"
        }
      ]
    }
  },
  "response": [
  ]
}