People API v2 Examples

Upload a user photo with JavaScript

Here is an example of using modern JavaScript to set a Claromentis user's profile photo.

It uses the web-native Fetch API to instruct the browser to:

  1. Download the image
  2. Send the image to the "Upload user profile photo" Claromentis endpoint as form data
// Source image
let imageUrl = "https://www.fillmurray.com/300/300";

// Claromentis hostname (e.g. acme.myintranet.com)
let hostname = "localhost";

// User ID to set a profile photo for
let userId = 2;

fetch(imageUrl)
    .catch(error => console.error('Error fetching image file', error))
    .then(response => response.blob())
    .catch(error => console.error('Error reading image file', error))
    .then(blob => {
        const file = new File([blob], 'image.jpg', {
            type: 'image/jpeg'
        });

        const formData = new FormData();
        formData.append("photo", file);

        return fetch(`https://${hostname}/api/people/v2/users/{$userId}/photo`, {
            method: 'POST',
            body: formData
        });
    })
    .then(response => console.log('Successfully uploaded profile photo', response, response.text()))
    .catch(error => console.error('Failed to upload profile photo', error));