SugarCRM 7 create custom Rest Api with Post method and call from record.js using App.api.call

I am creating a custom Rest v10 endpoint. I have created an API accepting GET method of request. Now I need to create an API with POST method, and send it data from my record.js file, which I have extended from contacts module. 

My API code is as following. 

My API is placed in:

custom\clients\base\api\MyContactsAPI.php

public function registerApiRest()
{
return array(
//GET
'MyContactsAPI' => array(
//request type
'reqType' => 'GET',

//set authentication
'noLoginRequired' => true,

//endpoint path
'path' => array('MyContactsAPI', 'GetData', '?'),
//endpoint variables
'pathVars' => array('', '', 'data'),
//method to call
'method' => 'GetDuplicates',
//short help string to be displayed in the help documentation
'shortHelp' => 'An API to get activity report of an account and all its related modules',
//long help to be displayed in the help documentation
'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
),
);
}
public function GetData($api, $args)
{
   // logic will be here
}

Now I don't know, how to convert the same API to accept POST requests and how to call it from record.js file using App.api.call and sending JSON data to API. 

Any help will be appriciated. 

Parents
  • I have managed to write a register API method using following code:

    public function registerApiRest()
    {
    return array(
    //GET
    'DupDetector' => array(
    //request type
    'reqType' => 'POST',

    //set authentication
    'noLoginRequired' => true,

    //endpoint path
    'path' => array('DupDetector'),
    //endpoint variables
    //'pathVars' => array(''),
    // Json parameters to the endpoint
    'jsonParams' => array('filter'),
    //method to call
    'method' => 'GetDuplicates',
    //short help string to be displayed in the help documentation
    'shortHelp' => 'An API to get activity report of an account and all its related modules',
    //long help to be displayed in the help documentation
    'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
    ),
    );
    }

    It is accepting JSON data when I try to run it using POSTMAN. But I don't know how to call it from record,js controller of backbone.js. 

    Any Help will be appreciated. 

  • Hi Atif  Hussain

    Mostly Alan Apter code will work, try by adding the code in record.js.

    If it did not helped you then let me know.

    Best Regards

    S Ramana Raju

Reply Children