How to get json data in REST endpoint

I have added an extra action to the recordlist view;

custom/modules/Opportunities/clients/base/views/recordlist/recordlist.js:

({
    extendsFrom: 'RecordlistView',
    
    initialize: function(options) {
        this._super("initialize", [options]);
        //add listener for custom button
        this.context.on('list:export2:fire', this.export2, this);
    },
    export2: function() {
        var selected = this.context.get("mass_collection").pluck('id');
        if (selected) {
            return App.api.call('read',
            App.api.buildURL('Opportunities/EngagementSalesExport'),
            {'selected_ids':selected},
            {
                success: function(response) {
                    console.log("SUCCESS");
                    console.log(response);
                },
                error: function(response) {
                    console.log('ERROR');
                    console.log(response);
                },
                complete: function(response){
                    console.log("COMPLETE");
                    console.log(response);
                },
                error: function(response){
                    console.log("ERROR");
                    console.log(response);
                }
            });
        }
    },
})

The tutorial here http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Extending_Endpoints/ Explains how to create an endpoint.

However it doesn't explain how to get the json data (the stringified array of selected ids);

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class OpportunitiesApi extends SugarApi
{
    public function registerApiRest()
    {
        return array(
            //GET
            'MyGetEndpoint' => array(
                //request type
                'reqType' => 'GET',

                //set authentication
                'noLoginRequired' => false,

                //endpoint path
                'path' => array('Opportunities', 'Export2'),

                //endpoint variables
                'pathVars' => array('', ''),

                //method to call
                'method' => 'Export2',

                //short help string to be displayed in the help documentation
                'shortHelp' => 'Export',

                //long help to be displayed in the help documentation
                'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
            ),
        );
    }

    /**
     * Method to be used for my MyEndpoint/GetExample endpoint
     */
    public function Export2($api, $args)
    {
        //how to access $args['selected_ids']?
    }

}

?>

$args contains

Array(
    [__sugar_url] => v10/Opportunities/Export2
)

Is it possible to access the json data?

  • Can you verify using your browser's developer tools (network tab) that the HTTP request is being sent correctly? That will help you figure out if it is a JavaScript problem or a server side one.

    One thing I noticed is that you are using a GET endpoint but are trying to pass data in the request body. This may cause a problem since GET requests typically do not have data in the request body which could cause it to be dropped. Request bodies are typically used with PUT and POST requests.

    Try passing your selected IDs as a URL parameter (which is a valid way of passing arguments on a GET request) or changing your endpoint to be a PUT or POST.

    App Ecosystem @ SugarCRM

  • Thanks!

    Changing call method to 'create' and the endpoint method to POST has fixed this; $args now contains

    Array
    (
        [selected_ids] => Array
            (
                [0] => 0124a524-accc-11e6-96a8-005056897bc3
            )
        [__sugar_url] => v10/Opportunities/Export2
    )