How is a POST API created and called?

8345.HelloWorldAPIv2.zip

I have attached a functioning GET and POST API, but I don't know how to view the json arguments sent to the API.

Example:the 'value': 'something to test' string. How do I get to that value in the API?.

        app.api.call('POST'app.api.buildURL('Hello/World/1'), {'value':'something to test'},
        {
            success: function(data){
                if(this.disposed){
                    return;
                }
                console.log(data);
                self.message = data;
                _.extend(selfself.message);
                self.render();
            },
            error: function (e) {
                console.log('error');
                console.log(e);
           }
        });
Parents
  • The result I get from the following is 'Hello World'.  I don't get anything from  value.

    Am I missing something?

    I am using the following API:

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

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

                    //set authentication
                    'noLoginRequired' => false,

                    //endpoint path will equal Hello/WorldPOST
                    'path' => array('Hello'),

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

                    //Json parameters to the end point
                    'jsonParams' => array('filter'),

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

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

                    //long help to be displayed in the help documentation
                    'longHelp' => '',

                ),

            );
        }

        /**
         * Method to be used for Hello/World/ endpoint
         */
        public function CallHelloWorldPOST($api, $args)
        {
            return 'Hello World ' . $args['value'];
        }


    }
    and the following json, but it is not displaying the value.
    ({
        plugins: ['Dashlet'],
        render: function() {
            console.log(this);
            this.loadData();
            //console.log(this.model.get('contract_status')); //If in contract record this works
            this._super("render");
        },

    /**
         * @inheritdoc
     */
        initialize: function(options) {
            // call the parent's (View's) initialize function
            // passing options as an array
            this._super('initialize', [options]);
            this.loadData();



        },

        loadData: function(options){
            if (_.isUndefined(this.model)){
                return;
            }
            var self = this;
        
            app.api.call('POST'app.api.buildURL('Hello'), {'value':'something to test'},
            {
                success: function(data){
                    if(this.disposed){
                        return;
                    }
                    console.log(data);
                    self.message = data;
                    _.extend(selfself.message);
                    self.render();
                },
                error: function (e) {
                    console.log('error');
                    console.log(e);
               }
            });
        }

    })
  • I think the problem lies in Javascript.

    SugarCRM uses CRUD approach for api calls, so you should not use POST/PUT/GET etc manually. You have to use create/read/update/delete.

    Eg:

    app.api.call('read', ... // GET

    app.api.call('create', ... // POST

    app.api.call('update', ... // PUT

    Anything that is not CRUD is considered GET, so your JSON is never sent to backend, which is why you didn't see the value in your API. If you change it to create, it should do a POST and you should be able to see the value in $args.

    I've tried to document some common snippets and usages here, in case it helps: https://seventunes.com/sugarcrm-javascript-snippets-1/#crud

Reply
  • I think the problem lies in Javascript.

    SugarCRM uses CRUD approach for api calls, so you should not use POST/PUT/GET etc manually. You have to use create/read/update/delete.

    Eg:

    app.api.call('read', ... // GET

    app.api.call('create', ... // POST

    app.api.call('update', ... // PUT

    Anything that is not CRUD is considered GET, so your JSON is never sent to backend, which is why you didn't see the value in your API. If you change it to create, it should do a POST and you should be able to see the value in $args.

    I've tried to document some common snippets and usages here, in case it helps: https://seventunes.com/sugarcrm-javascript-snippets-1/#crud

Children