I need assistance in changing a file-type field for a CRM contact using the Bitrix24 API. Specifically, I’m trying to replace a contact’s image, but I can’t find relevant information in the Bitrix24 documentation. I have this request structure:
{{url}}/{{token}}/crm.contact.update.json?{{params}}
I attempted to send the image as a base64-encoded string through parameters and as form-data, but while other fields update successfully, the image remains unchanged. I’m unsure if there’s a prerequisite process required to execute this correctly.
For illustration, this is what I’m sending for the image update:
fields[UF_CRM_QR_CODE][fileData] = ["image.png", "{{base64}}"]
Hi HappyDancer99,
When working with the Bitrix24 API to update file fields such as CRM contact images, using the right data format is crucial. Bitrix24 mandates the use of multipart/form-data for uploading file data instead of base64-encoded strings directly.
Here's a streamlined way to update an image:
- Ensure your image is converted to a suitable format, excluding extras like
data:image/png;base64,
.
- Utilize a Node.js library, like
axios
, which supports sending requests as multipart/form-data.
An example using axios
might look like this:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const formData = new FormData();
formData.append('ID', '');
formData.append('fields[UF_CRM_QR_CODE]', fs.createReadStream('/path/to/image.png'));
axios.post('https:///rest//crm.contact.update.json', formData, {
headers: formData.getHeaders()
})
.then(response => {
console.log('Image Updated:', response.data);
})
.catch(error => {
console.error('Error:', error);
});
Change contact_id
and the file path to match your specific data. Ensure that the field UF_CRM_QR_CODE
is correct for image storage in Bitrix24. Using Node.js & axios
efficiently handles multipart requests, facilitating a smooth update process.
Hi HappyDancer99, modifying file fields in Bitrix24 CRM can often be simplified by understanding how the API processes files. Based on your experience, we can explore another method to ensure successful image uploading.
Firstly, recognize that Bitrix24's API expects file uploads to be in the form of multipart/form-data, not as base64 strings. This is a common requirement when dealing with file uploads in many APIs, ensuring data is transferred efficiently without format issues.
Here's a revised approach using axios
and form-data
in Node.js:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
// Create a FormData instance
const formData = new FormData();
// Add the contact ID
formData.append('ID', '');
// Append the image file
formData.append('fields[UF_CRM_QR_CODE]', fs.createReadStream('/path/to/image.png'));
// Send the request
axios.post('https:///rest//crm.contact.update.json', formData, {
headers: formData.getHeaders()
})
.then(response => {
console.log('Image Updated:', response.data);
})
.catch(error => {
console.error('Error:', error);
});
In this example, replace <contact_id>
and <url>
with the actual contact ID and your API endpoint URL.
Be sure the field UF_CRM_QR_CODE
aligns with your CRM configuration; it's possible that a different field name is set up for storing images in your environment.
This method leverages Node.js's ability to manage file streams, ensuring the data is correctly formatted for Bitrix24. By focusing on multipart data, this solution overcomes the limitations of directly using base64 encoding.