Hi everyone! I’m experiencing difficulty with a workflow where I need to take an image file from an HTTP request and convert it to base64 format so I can send it to Odoo. I’ve been struggling with this for a while but can’t get it to work as expected.
I’m downloading an image from an API and trying to process it in a Code node. Here’s the code I have:
// Retrieve binary content from the previous step
const imageData = $input.first().binary?.attachment;
if (!imageData) {
throw new Error("Binary content not available");
}
// Convert to base64 format
const encodedImage = imageData.data;
// Get today’s date
const currentDate = new Date().toISOString().substring(0, 10);
// Prepare output for the next node
return {
json: {
photo_field: encodedImage,
date_field: currentDate,
notes_field: $json.notes_field || "",
},
binary: {
attachment: imageData
}
};
The image comes from an API call and has the following properties:
- Filename: random_generated_name
- Path: api/media/2025/1/123456
- Extension: jpg
- Content-Type: image/jpeg
- Size: 45.2 kB
Can anyone help me understand what I might be doing wrong? The base64 conversion doesn’t seem to work correctly when I attempt to upload to Odoo.
Double check you’re using the right field name in Odoo. The API sometimes expects different naming than what you see in the UI. Also log imageData.data to see if it’s actually base64 or if n8n is giving you buffer data that needs converting first.
Had the same issue recently. Fixed it by checking how n8n handles the buffer data - it stores binary data differently depending on where it comes from. Try this in your Code node:
const binaryData = $input.first().binary?.attachment;
if (!binaryData) {
throw new Error("No binary data found");
}
// Handle both buffer and base64 cases
let base64String;
if (Buffer.isBuffer(binaryData.data)) {
base64String = binaryData.data.toString('base64');
} else {
base64String = binaryData.data;
}
const currentDate = new Date().toISOString().substring(0, 10);
return {
json: {
photo_field: base64String,
date_field: currentDate,
notes_field: $json.notes_field || ""
}
};
This covers cases where n8n passes buffer data instead of base64. Also turn on debug mode in your HTTP Request node - you’ll see exactly what format the binary data is when it hits your workflow.
I hit the same issue with Odoo’s image fields. You’re probably passing the base64 data without the MIME type prefix that Odoo needs. Odoo’s binary fields expect the base64 string to include the data URI scheme. Here’s how to fix it: const imageData = $input.first().binary?.attachment; if (!imageData) { throw new Error(“Binary content not available”); } // Add the MIME type prefix for Odoo const mimeType = imageData.mimeType || ‘image/jpeg’; const encodedImage = data:${mimeType};base64,${imageData.data}; const currentDate = new Date().toISOString().substring(0, 10); return { json: { photo_field: encodedImage, date_field: currentDate, notes_field: $json.notes_field || “”, }}; Also check that your Odoo field is set as Binary, not Image - they handle base64 differently in some versions.