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. 

  • Try the following snippet in record.js:

    App.api.call('create', App.api.buildURL('DupDetector'), {"parameter1":"test", "parameter2": "test2"}, {
                success: function (data) {

                },
                error: function (e) {
                    throw e;
                }
    });

    If you give me more context of what you're trying to do, I can give you more detail. Hope this helps.

  • Hi Alan Apter

    Thanks for your kind reply. 

    This code works perfectly to my needs. 

    And sorry for the late response. 

    Regards,

    Atif Hussain

Reply Children
No Data