How to upload picture API REST V4.1

Version 7.11.13
Sugar Version 6.5.25

I have a custom module and I am uploading information through the suitecrm REST API v4.1, in this module there is a field for an image but I cannot find how to upload it correctly.

Here is the example of my code:

$set_entry_parameters_mascota = array(
     //session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => " <Module custom>",
 //Record attributes
"name_value_list" => array(
	array('name' => 'name', 'value' => $data),
	array('name' => 'description', 'value' => $data),
	array('name' => 'especie', 'value' => $data),
	array('name' => 'raza', 'value' => $data),
	array('name' => 'color', 'value' => $data),
	array('name' => 'color2', 'value' => $data),
	array('name' => 'genero', 'value' => $data),
	array('name' => 'caracter', 'value' => $data),
	array('name' => 'segmento_c', 'value' => $data),
	array('name' => 'foto', 'value' => ) //this field in the database appears as a varchar so I 
              don't understand exactly how to upload it
 ),
 );

$set_entry_result_mascota = call(“set_entry”, $set_entry_parameters_mascota, $url);

If someone has a source where I can find information on how to upload it or an example of how it is done, they would help me a lot.
Thank you.

Hi,

There is no API for that but you can extend the API with this function:

	function set_image($session,$module_name,$id,$field,$contents,$deleted=0) {
		global $sugar_config;
		$error = new SoapError();
		if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'write', 'no_access', $error)) {
			$GLOBALS['log']->info('End: SugarWebServiceImpl->set_image');
			return;
		} // if

		$filename = $sugar_config['upload_dir'].$id.'_'.$field;
		if ($deleted == 0) {			
			$contents = base64_decode($contents);
			
			file_put_contents($filename,$contents);
		}
		else {
			if (file_exists($filename)) {
				unlink($filename);
			}
		}
		$GLOBALS['log']->info('End: SugarWebServiceImpl->set_image');
		return array('id'=>$id, 'entry_list' => array('filename'=>$filename));
	}

Then, after calling the set_entry API, you can call that set_image API.
The parameters should be:
“session” => $session_id,
“module_name” => " Module custom",
“id” => “The id of the record as returned by set_entry”,
“field” => " foto",
“contents” => “The file contents (base64 encoded)”,

1 Like

Thank you very much for answering me, I understand.

Could you tell me where I have to integrate this function that you provide me to extend the REST api.

search inside custom / modules /
and in services but I don’t specifically locate how I should integrate this function.

Thank you

Everything is explained at

Hello, thanks for answering, reviewing the feedback you gave me and implementing, but I got an error that I could not solve the structure of what I did this way:
directori created:

// service / v4_1_custom
records:

Registry.php

  require_once 'service/v4_1/registry.php';
    class registry_v4_1_custom extends registry_v4_1
    {
        protected function registerFunction()
        {
            parent::registerFunction();
            $this->serviceClass->registerFunction('set_image',
                                                  array(
                                                    'module_name'=>'xsd:string',
                                                    'id'=>'xsd:string',
                                                    'field' => 'xsd:string',
                                                    'contents' => 'xsd:string'),
                                                  array(
                                                    'return'=>'tns:field[]')
                                                  );
        }
    }
?>

rest.php

    chdir('../../..');

require_once 'SugarWebServiceImplv4_1_custom.php';

$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';

require_once 'service/core/webservice.php';

soap.php

    chdir('../../..');
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_class = 'SugarSoapService2';
$webservice_path = 'service/v2/SugarSoapService2.php';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_class = 'registry_v4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$location = 'custom/service/v4_1_custom/soap.php';
require_once('service/core/webservice.php');

//////////////////////////////////////////////////////////////////////
SugarWebServiceImplv4_1_custom.php
SugarWebServiceUtilv4_1_custom.php
just copy the existing files and add the custom.
//////////////////////////////////////////////////////////////////////
Add the function also to the SugarWebServiceImpl.php file

but in console I get this error:

Fatal PHP error: ArgumentCountError not caught: Too few arguments to work SugarWebServiceImpl :: set_image (), 3 passed in /<crm> / service / core / REST / SugarRestJSON.php on line 94 and at least 5 expected in /<crm>/service/core/SugarWebServiceImpl.php:1203
Stack Tracking:
# 0 /<crm>/service/core/REST/SugarRestJSON.php(94): SugarWebServiceImpl-> set_image ('80e29ec3455b22e ...', 'GRAL_Mascota', Array)
# 1 /<crm>/service/core/SugarRestService.php(136): SugarRestJSON-> serve ()
# 2 /<crm>/service/core/webservice.php(70): SugarRestService-> serve ()
# 3 /<crm>/service/v4_1/rest.php(56): require_once ('')
# 4 {main}
  launched at /<crm>/service/core/SugarWebServiceImpl.php on line 1203

It looks like you didn’t call the API with the correct parameters.
I see Array where there should be id, field, contents.

1 Like