Fetching Base64 from an Image URL in Celigo

Hi everyone,

I'm currently working on a project where I need to fetch an image from a specific URL and convert it into a base64 encoded string using Celigo. Despite my efforts with GET requests through either a standard HTTP connector or a custom API in Celigo, I haven't been successful yet.

I'm reaching out to see if anyone in the community has faced and overcome a similar challenge. If you have any insights, tips, or a snippet of code that could guide me in the right direction, it would be immensely helpful. Also, if there are any best practices for handling image data within Celigo, I'd be eager to learn about those as well.

0

Comments

6 comments
Date Votes
  • Hi Nuri,

    Could you please share a screenshot of your flow? I'm interested in understanding the use case and the setup that you have configured so that I can offer assistance.

    You can use a custom script (Hooks) to convert the response body. Please provide the key path, and then utilize the ".toString('base64');" method. I can offer more details once I review your setup

    Thanks,
    Om Mishra


    0
  • Hi Om Mishra,

    Thank you for reaching out. The integration flow I'm currently handling is quite complex. The specific aspect I'm focusing on involves retrieving data from an external API. This data includes URLs for product images, an example being https://example.com/image.png.

    My primary challenge is converting these image URLs into base64 encoded strings. 

    Best regards,
    Nuri Ensing

    0
  • Nuri Ensing there isn't currently a way to natively do this in the platform, but it is on the roadmap. In the meantime, there are a couple ways you could do this.

     

    If you have a NetSuite instance you can work with, then what I've done in the past is have IO drop the file into NetSuite's file cabinet then have another flow or a lookup afterwards to run a saved search of the file, then run a SuiteScript hook to convert the file to base64 string before it passes back to IO in the saved search response. This sample script is a preMap SuiteScript, but you would probably need to convert it to preSend SuiteScript. 

    https://docs.celigo.com/hc/en-us/articles/360050485392-SuiteScript-hooks-overview

    https://github.com/CeligoServices/SuiteScript-Hooks

    /**
     * Called from integrator.io prior to mapping the data
     * @param  {Object} options The data sent to the hook.  See SampleOptionsData.json for format
     * @return {Array}         Array containing the data sent to the mapper
     */
    var preMapHook = function(options){
    	
    	//The array that will be returned from this hook to be processed by the mappings
    	var response = [];
    
    	for (var i = 0; i < options.data.length; i++) {
    		/* The response object contains of a data array and an error array
    		The data array is the elements that will be passed on to the mappings
    		The errors array will show up as failed records on the integrator.io dashboard.  
    		Each element in the array may consist of one or more errors
    		*/
    		
    		try {
    			if (options.data[i].package_data.LabelImageFormat == 'JPEG' || options.data[i].package_data.LabelImageFormat == 'JPG') {
    				options.data[i].package_data.LabelImageFormat = 'JPGIMAGE';
    			} else if (options.data[i].package_data.LabelImageFormat == 'PNG') {
    				options.data[i].package_data.LabelImageFormat = 'PNGIMAGE';
    			}
    			var fileName = options.data[i].document_number + '-' + options.data[i].package_data.Packages[0].TrackingNumber + '.' + options.data[i].package_data.LabelImageFormat.toLowerCase();
    			var file = nlapiCreateFile(fileName,options.data[i].package_data.LabelImageFormat,options.data[i].package_data.LabelImages);
    			file.setFolder(options.data[i].netsuite_folder_id);
    			file.setName(fileName);
    			var fileId = nlapiSubmitFile(file);
    			nlapiAttachRecord('file',fileId,options.data[i].record_type,options.data[i].internal_id);
    
    			response.push({
    				data : JSON.parse(JSON.stringify(options.data[i])),
    				errors : []
    			});
    		} catch (e) {
    			nlapiLogExecution('ERROR', e.name, e.message);
    			response.push({
    				data : null,
    				errors : [{
    					code : e.name,
    					message : e.message
    				}]
    			});
    		}
    	}
    
    	try {
    		logOptions(options, response);
    	} catch (e) {
    		nlapiLogExecution('ERROR', e.name, e.message);
    		/*In the case of a high level failure all records should be marked as failed
    		If there is a single record failure the individual error should be logged in the function called
    		within the try-catch block
    		*/
    		for (var i = 0; i < response.length; i++) {
    			response[i].data = null;
    			response[i].errors.push({
    				code : e.name,
    				message : e.message
    			});
    		}
    	}
    
    	//Send the data to the mappings
    	return response;
    
    };
    
    /**
     * Log the data passed into the hook into the SuiteScript logs of the RESTlet
     * @param  {Object} options  Data passed into the PreMap hook
     * @param  {Array} response The object that is passed on to the mappings
     * @return null
     */
    var logOptions = function(options, response){
    	nlapiLogExecution('AUDIT', 'PreMap Options', JSON.stringify(options));
    };

     

    You could use a lambda function. Youssef Zouhairi wrote up post a couple months back on how to do this.
    https://docs.celigo.com/hc/en-us/community/posts/18789295111963-FAQ-Convert-a-blob-key-to-base64-string-for-import-

    1
  • Hi Tyler Lamparter, I'm facing the same situation as Nuri... But I have not NetSuite connectors at all. My implementation revolves around Business Central.

    How could I get a picture using its URL and converting it to a base64 encoded string, using the Integrator.io flows?

     

    0
  • Marcelo Borges what's your use case for needing to convert it to base64 string? What endpoint are you going to that requires a base64 string and can you send me the api docs for it? I just want to make sure I don't have a different option for you.

    0
  • Hi Tyler Lamparter my use case is the following: We store Item images as URL references in Business Central, and we needed to send those as encoded base64 pictures to the Shopify Product API. But I've JUST realized, Shopify actually has their own way of taking a URL and downloading the pictures themselves. So, I guess I don't really have a use case for now.  🤯

    Thanks for the prompt response!

    1

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post