Sample code for 30+ languages & platforms
Xbase++

MYOB: Add a Category

See more MYOB Examples

Sends a POST to add a general ledger category.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL nRespStatusCode

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

//  Implements the following CURL command:

//  curl --request POST "https://api.myob.com/accountright/c06778dd-4371-4a83-975f-522df65d7574/GeneralLedger/Category" \
//    --header "Authorization: Bearer ACCESS_TOKEN" \
//    --header "x-myobapi-key: {{myob_api_key}}" \
//    --header "x-myobapi-version: v2" \
//    --header "Accept-Encoding: gzip,deflate" \
//    --data "{
//  \"DisplayID\" : \"CAT002\",
//  \"Name\" : \"Victoria\",
//  \"Description\" : \"Demo API Category Endpoint.\",
//  \"IsActive\" : true
//  }"

//  Use this online tool to generate code from sample JSON:
//  Generate Code to Create JSON

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

//  {
//    "DisplayID": "CAT002",
//    "Name": "Victoria",
//    "Description": "Demo API Category Endpoint.",
//    "IsActive": true
//  }

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("DisplayID", "CAT002")
oJson:UpdateString("Name", "Victoria")
oJson:UpdateString("Description", "Demo API Category Endpoint.")
oJson:UpdateBool("IsActive", 1)

oHttp:SetRequestHeader("Authorization", "Bearer ACCESS_TOKEN")
oHttp:SetRequestHeader("x-myobapi-key", "{{myob_api_key}}")
oHttp:SetRequestHeader("Accept-Encoding", "gzip,deflate")
oHttp:SetRequestHeader("x-myobapi-version", "v2")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.myob.com/accountright/c06778dd-4371-4a83-975f-522df65d7574/GeneralLedger/Category", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode != 200)
    ? "Response Header:"
    ? oResp:Header
    ? "Response Body:"
    ? oResp:BodyStr
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

? "Success."

oHttp:destroy()
oJson:destroy()
oResp:destroy()