Uploading User Profile Photos using the People API

Overview

This is a technical guide with an example on how to upload or update users profile picture in Claromentis using API. 

Requirements

  • Claromentis v8.9+ with People API v2
  • Knowledge of JavaScript REST API

Method

A multipart/form-data HTTP request must be made to the API, with an image included as part of the photo field as described in this documentation.

It expects an image file to be uploaded as part of this form data, rather than a URL to the file.

For instance, using JavaScript code you could first request the file you want to upload and use that in the form data of a second request to the People API.

Example code

fetch("https://someurl.com/images/101.jpg")
    .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://mycompany.myintranet.com/api/people/v2/users/101/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));

 

Replace https://someurl.com/images/101.jpg with the URL of the image source and

Replace https://mycompany.myintranet.com/api/people/v2/users/101/photo with the actual Fetch URL required.

You may want to use variables, or even an HTML form, to adjust the image source URL and upload URL for different users, depending on your use case.

Please Note: this is modern Javascript that won't work in Internet Explorer.

Created on 23 February 2021 by Chris Andrew. Last modified on 24 February 2021
api

Was this helpful?  

2 Likes
Share