Uploading Avatars with API

I’m about to set out on the task of trying to sync leader photos to troop track via API call. Anyone have any experience this part of the API? Even better, a sample of a successful POST request?

It’s not clear to me if I need to POST the actual image data or a url where the image data resides.

1 Like

I think I have this figured out. It appears that one needs to POST to /v1/users/{id} a json structure of

{
“user” : {
“profile_photo_file_name” : PROFILE_PHOTO_FILE_NAME,
“profile_photo_file_content” : PROFILE_PHOTO_FILE_CONTENT
}
}

where PROFILE_PHOTO_FILE_NAME is the file name and PROFILE_PHOTO_FILE_CONTENT is the base64 encoded data from the file.

An example, in PHP:

$user_id = 12345;
$image_path = 'path_to_image.jpg';

$headers = array(
	'X-Partner-Token: XXXXXXX', 
	'X-User-Token: XXXXXXX',
);
$api_base = "https://XXXXXXX.trooptrack.com:443/api/v1/";

$url = 'users/'.$user_id;
$post_obj = new stdClass;
$post_obj->user = new stdClass;

$post_obj->user->profile_photo_file_name = basename($image_path);
	
$image_data = file_get_contents($image_path);	
$post_obj->user->profile_photo_file_content = base64_encode($image_data);


$ch = curl_init();


$data_string = json_encode($post_obj);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  

$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_URL, $api_base.$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);		

$string = curl_exec($ch);

curl_close($ch);	

$result = json_decode($string);
1 Like