Chilkat Online Tools

PureBasic / Sunshine Conversations API / Update User

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.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

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "username")
    CkHttp::setCkPassword(http, "password")

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

    ; The following JSON is sent in the request body.

    ; {
    ;   "signedUpAt": "2020-05-21T15:53:30.197Z",
    ;   "profile": {
    ;     "givenName": "Jane",
    ;     "surname": "Doe",
    ;     "email": "jane.doe@gmail.com",
    ;     "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    ;   },
    ;   "metadata": {
    ;     "lang": "en-ca"
    ;   }
    ; }

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

    CkJsonObject::ckUpdateString(json,"signedUpAt","2020-05-21T15:53:30.197Z")
    CkJsonObject::ckUpdateString(json,"profile.givenName","Jane")
    CkJsonObject::ckUpdateString(json,"profile.surname","Doe")
    CkJsonObject::ckUpdateString(json,"profile.email","jane.doe@gmail.com")
    CkJsonObject::ckUpdateString(json,"profile.avatarUrl","https://s3.amazonaws.com/avatar.jpg")
    CkJsonObject::ckUpdateString(json,"metadata.lang","en-ca")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

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

    CkJsonObject::ckEmitSb(json,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PATCH","https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

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

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

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

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "user": {
    ;     "id": "7494535bff5cef41a15be74d",
    ;     "externalId": "your-own-id",
    ;     "signedUpAt": "2020-05-21T15:53:30.197Z",
    ;     "profile": {
    ;       "givenName": "Jane",
    ;       "surname": "Doe",
    ;       "email": "jane.doe@gmail.com",
    ;       "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    ;     },
    ;     "metadata": {
    ;       "lang": "en-ca"
    ;     }
    ;   }
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    Id.s = CkJsonObject::ckStringOf(jResp,"user.id")
    ExternalId.s = CkJsonObject::ckStringOf(jResp,"user.externalId")
    SignedUpAt.s = CkJsonObject::ckStringOf(jResp,"user.signedUpAt")
    GivenName.s = CkJsonObject::ckStringOf(jResp,"user.profile.givenName")
    Surname.s = CkJsonObject::ckStringOf(jResp,"user.profile.surname")
    v_Email.s = CkJsonObject::ckStringOf(jResp,"user.profile.email")
    AvatarUrl.s = CkJsonObject::ckStringOf(jResp,"user.profile.avatarUrl")
    Lang.s = CkJsonObject::ckStringOf(jResp,"user.metadata.lang")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X PATCH
	-u 'username:password'
	-H "Content-Type: application/json"
	-d '{
    "signedUpAt": "2020-05-21T15:53:30.197Z",
    "profile": {
        "givenName": "Jane",
        "surname": "Doe",
        "email": "jane.doe@gmail.com",
        "avatarUrl": "https://s3.amazonaws.com/avatar.jpg"
    },
    "metadata": {
        "lang": "en-ca"
    }
}'
https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId

Postman Collection Item JSON

{
  "name": "Update User",
  "_postman_id": "8b3b25b1-b89b-4a6d-8aae-e72fd279cbc7",
  "request": {
    "method": "PATCH",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
    },
    "url": {
      "raw": "{{url}}/v2/apps/{{appId}}/users/:userIdOrExternalId",
      "host": [
        "{{url}}"
      ],
      "path": [
        "v2",
        "apps",
        "{{appId}}",
        "users",
        ":userIdOrExternalId"
      ],
      "variable": [
        {
          "key": "userIdOrExternalId"
        }
      ]
    },
    "description": "Updates a user."
  },
  "response": [
    {
      "id": "386032bc-c4a4-4590-a4be-47f5a73f20ba",
      "name": "User not found",
      "originalRequest": {
        "method": "PATCH",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/users/:userIdOrExternalId",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "users",
            ":userIdOrExternalId"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "userIdOrExternalId"
            }
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "cdd035df-8db2-4fe8-95fa-337219ee4c48",
      "name": "Ok",
      "originalRequest": {
        "method": "PATCH",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n    \"profile\": {\n        \"givenName\": \"Jane\",\n        \"surname\": \"Doe\",\n        \"email\": \"jane.doe@gmail.com\",\n        \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n    },\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/users/:userIdOrExternalId",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "users",
            ":userIdOrExternalId"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "userIdOrExternalId"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"user\": {\n  \"id\": \"7494535bff5cef41a15be74d\",\n  \"externalId\": \"your-own-id\",\n  \"signedUpAt\": \"2020-05-21T15:53:30.197Z\",\n  \"profile\": {\n   \"givenName\": \"Jane\",\n   \"surname\": \"Doe\",\n   \"email\": \"jane.doe@gmail.com\",\n   \"avatarUrl\": \"https://s3.amazonaws.com/avatar.jpg\"\n  },\n  \"metadata\": {\n   \"lang\": \"en-ca\"\n  }\n }\n}"
    }
  ]
}