Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Pallavi N
    Custom API - post Method
    Topic posted March 8, 2017 by Pallavi NBlue Ribbon: 750+ Points, last edited March 23, 2017 
    230 Views, 27 Comments
    Title:
    Custom API - post Method
    Summary:
    Custom API - post Method
    Content:

    Hi,

    I created a custom API which has an endpoint with POST method to call a connector API ( connects to EBS R12). Here is the code I am using

    service.post('/mobile/custom/ExpenseHeaderDetCust/HeaderDet', function (req, res) {

      console.log('getting ready to call POST /mobile/custom/ExpenseHeaderDetCust/HeaderDet');

      var sdk = req.oracleMobile;

      var optionList = {

      //uri: '/mobile/connector/ExpenseHeaderDetails'

      };

      var jsnObj = {

      Body: {

      "ExpenseCount": {

      "@xmlns": "http://vis1225.dpebs-server.com:8000/webservices/rest/ExpenseCount/get_smart_exp_cnt_dtls_f",

      "RESTHeader": {

      "xmlns": "http://vis1225.dpebs-server.com:8000/webservices/rest/ExpenseCount/header",

      "Responsibility": "SYSTEM_ADMINISTRATOR",

      "RespApplication": "SYSADMIN",

      "SecurityGroup": "STANDARD",

      "NLSLanguage": "AMERICAN",

      "Org_Id": "204"

      },

      "InputParameters": {

      "P_REPORT_HEADER_ID": "37345"

      }

      }

      }

      };

      optionList.body = JSON.stringify(jsnObj);

      optionList.headers = {'Content-Type': 'application/json;charset=UTF-8'};

      optionList.headers = {'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134'};

      optionList.headers = {'Authorization': 'SU5BTEdBUllUTVRSSUFMX0RFVkVMT1BNRU5UMV9NT0JJTEVfQU5PTllNT1VTX0FQUElEOkFyZzJlanZfc3oxcW1h'};

      sdk.connectors.post('ExpenseHeaderDetails',optionList, function (error, response, body) {

      if (error) {

      res.send(response, error.message);

      } else {

      res.setHeader({'Content-Type': 'application/json;charset=UTF-8'});

      res.send(200, body);

      }

      });

      });

    };

    And I get this error when I test the custom API with status code 500

    {

      "message": "Custom Code Problem in callback: [object Object]"

    }

    Can anyone help to figure out, what might be wrong?

    Comment

    • Pallavi N

      Hi Tware,

      Identity Domain: inalgarytmtrial1

      Thanks,

      Pallavi.

    • Tom Ware

      I see the following exception in your log:

      Error: Target version for /mobile/connector//mobile/connector/ExpenseHeaderDetails/[object Object] not specified in custom code. When you call custom code or a connector from within custom code, you must specify the target version either in the oracleMobile section of your package.json or in the http header: oracle-mobile-api-version.

          at populateHeaders (/mobile/mobile_ccc/mcs-node-router/rest.js:268:10)

          at populateBasicOptions (/mobile/mobile_ccc/mcs-node-router/rest.js:283:15)

          at populateOptions (/mobile/mobile_ccc/mcs-node-router/rest.js:215:10)

          at buildRequestAsync (/mobile/mobile_ccc/mcs-node-router/rest.js:208:27)

          at Object.create.postAsync (/mobile/mobile_ccc/mcs-node-router/rest.js:134:11)

          at Object.create.wrap (/mobile/mobile_ccc/mcs-node-router/utils.js:53:62)

          at postFunction (/mobile/mobile_ccc/mcs-node-router/namedAPI.js:66:17)

          at /mobile/mobile_ccc/custom_code_modules/expenseheaderdetails_1.0.0_4/expenseheaderdetails/expenseheaderdetails.js:52:31

          at callbacks (/mobile/mobile_ccc/mcs-node-router/node_modules/express/lib/router/index.js:164:37)

          at param (/mobile/mobile_ccc/mcs-node-router/node_modules/express/lib/router/index.js:138:11)

          at pass (/mobile/mobile_ccc/mcs-node-router/node_modules/express/lib/router/index.js:145:5)

          at Router._dispatch (/mobile/mobile_ccc/mcs-node-router/node_modules/express/lib/router/index.js:173:5)

          at Object.router (/mobile/mobile_ccc/mcs-node-router/node_modules/express/lib/router/index.js:33:10)

          at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:193:15)

          at Object.mcsSdk (/mobile/mobile_ccc/mcs-node-router/mcs-node-server-new.js:867:9)

          at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:193:15)

      ]]

      You need to specify the connector version to use: Calling MCS APIs from Custom Code .  I am surprised you didn't see an error message like this in your response.

    • Pallavi N

      Hi Tware ,

      I included the version in headers and also in package.json.

      This is my code

      service.post('/mobile/custom/ExpenseHeaderDetails/HeaderDet', function (req, res) {

        //var sdk = req.oracleMobile;

        var body = {

        Header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=',

        'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134',

        'oracle-mobile-uitooling-backend-name': 'Test',

        'oracle-mobile-uitooling-backend-version': '1.0',

        'oracle-mobile-api-version':'1.0'

        },

        Body: {

        'SmartExp1': {

        '@xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/get_smart_exp_cnt_dtls_f',

        'RESTHeader': {

        'xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/header',

        'Responsibility': 'SYSTEM_ADMINISTRATOR',

        'RespApplication': 'SYSADMIN',

        'SecurityGroup': 'STANDARD',

        'NLSLanguage': 'AMERICAN',

        'Org_Id': '204'

        },

        'InputParameters': {

        'P_REPORT_HEADER_ID': '37345'

        }

        }

        }

        };

        var optionList = {

        inType: 'json',versionToInvoke: '1.0',outType:'json'

        };

        req.oracleMobile.connectors.post('ExpenseHeaderDetails', body, optionList).then(

        function (result) {

        res.send(result.statusCode, result.result);

        },

        function (error) {

        res.send(500, error.error);

        }

        );

        });

      The ERROR I am getting is

      error.png

      Thanks,

      Pallavi.

    • Tom Ware

      The code above does not correspond to what you are running, your logs have this string: ''ERROR Block:Entered" and I don't see it in the code above.

      To diagnose the error, I suggest you do the following.

      Where you currently output 'ERROR Block:Entered'.  Add the following line:

      console.dir(error)

      And check the MCS log pane under administration to see the error.

      BTW: Unless you are calling a connector on a different backend, you should not need any of the following:

        'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=',

        'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134',

        'oracle-mobile-uitooling-backend-name': 'Test',

        'oracle-mobile-uitooling-backend-version': '1.0',

      BTW: Are you connecting to a REST connector or a SOAP connector.  Your payload above is SOAP specific.

    • Pallavi N

      Hi Tware,

      I added console.dir(error) as you suggested.Log file shows the following error

      { error: [Error: Argument error, options.body.] }

      I am connecting to a REST Connector( for  REST Service exposed from a PLSQL package using Integrated SOA Gateway).

      Thanks,

      Pallavi.

    • Tom Ware

      Ok....  Lets take another look at how we can start building your call.

      You likely start with the doc here:

      Calling MCS APIs from Custom Code

      I think there are some parts of that doc we can improve (I'll kick that off soone)

      The first 2 examples in the doc are for SOAP connectors - and that is the format you are using.

      i.e.

      var body = {

        HEADER: { ...},

        BODY: {...}

      }

      Let's start instead with this example, instead which uses a REST connector:

      req.oracleMobile.connectors.googlemaps.get('directions/json', null,

      {qs: {origin: '45+Oconner,+Ottawa',destination: 'Toronto'}}).then(

        function(result){

        res.send(result.statusCode, result.result);

        },

        function(error){

        res.send(500, error.error);

        }

      );

      You'll need to change your call to use post instead which looks like this:

      post(resourceName, object, options, httpOptions)

      You'll package your body slightly differently when you make this call.  "object" above is the body and the headers will get packaged in the "httpOptions".

      Very roughly:

      var body =

      {

        'SmartExp1': {

        '@xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/get_smart_exp_cnt_dtls_f',

        'RESTHeader': {

        'xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/header',

        'Responsibility': 'SYSTEM_ADMINISTRATOR',

        'RespApplication': 'SYSADMIN',

        'SecurityGroup': 'STANDARD',

        'NLSLanguage': 'AMERICAN',

        'Org_Id': '204'

        },

        'InputParameters': {

        'P_REPORT_HEADER_ID': '37345'

        }

        }

        };

      var headers =

      {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=',

        'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134',

        'oracle-mobile-uitooling-backend-name': 'Test',

        'oracle-mobile-uitooling-backend-version': '1.0',

        'oracle-mobile-api-version':'1.0'

        }

      req.oracleMobile.connectors.<yourconnectorname>.get('ExpenseHeaderDetails', body,

      null, {headers: headers}).then(

      etc....

    • Pallavi N

      Hi Tware,

      I made the code changes as you suggested. I still see the same ERROR

      { error: [Error: Argument error, options.body.] }

      The Connector is working fine.

      Is this because of the post method call? Or with the Request (payload)?

      Thanks,

      Pallavi

    • Pallavi N

      Hi Tware,

      And I also see the following in log file

      { headers: { date: 'Tue, 21 Mar 2017 06:23:53 GMT', 'content-length': '0', 'x-oracle-dms-ecid': '005InUk6YLdA9T3_RlXBid0001AO000039', 'www-authenticate': 'Basic realm=owsm' }, contentLength: '0', statusCode: 401 }

      But I passed the 'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=' header which is required for my REST Endpoint.

      And also in my custom API security page, LogIn Required is disabled.

      Thanks,

      Pallavi

    • Tom Ware

      Try removing the 'Authorization' line from your headers and putting the value in "externalAuthorization" in the options object.

      Roughly:

      req.oracleMobile.connectors.<yourconnectorname>.get('ExpenseHeaderDetails', body,

      {externalAuthorization: 'Basic c3lzYWRtaW46b3JhY2xlMTI='}, {headers: headers}).then(

      If that doesn't help, please post your current code.

    • Pallavi N

      Hi Tware,

      Unfortunately, this didn't work. Here is my code

      /**

      * The ExpressJS namespace.

      * @external ExpressApplicationObject

      * @see {@link http://expressjs.com/3x/api.html#app}

      */

      /**

      * Mobile Cloud custom code service entry point.

      * @param {external:ExpressApplicationObject}

      * service

      */

      module.exports = function (service) {

        /**

        *  The file samples.txt in the archive that this file was packaged with contains some example code.

        */

        service.post('/mobile/custom/ExpenseHeaderDetails/HeaderDet', function (req, res) {

        //var sdk = req.oracleMobile;

        /*var body = {

        Header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=',

        //'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134',

        //'oracle-mobile-uitooling-backend-name': 'Test',

        //'oracle-mobile-uitooling-backend-version': '1.0',

        'oracle-mobile-api-version':'1.0'

        },

        Body: {

        'SmartExp1': {

        '@xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/get_smart_exp_cnt_dtls_f',

        'RESTHeader': {

        'xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/header',

        'Responsibility': 'SYSTEM_ADMINISTRATOR',

        'RespApplication': 'SYSADMIN',

        'SecurityGroup': 'STANDARD',

        'NLSLanguage': 'AMERICAN',

        'Org_Id': '204'

        },

        'InputParameters': {

        'P_REPORT_HEADER_ID': '37345'

        }

        }

        }

        };*/

        //console.log(body);

        var body =

        {

         'SmartExp1': {

         '@xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/get_smart_exp_cnt_dtls_f',

         'RESTHeader': {

         'xmlns': 'http://vis1225.dpebs-server.com:8000/webservices/rest/SmartExp1/header',

         'Responsibility': 'SYSTEM_ADMINISTRATOR',

         'RespApplication': 'SYSADMIN',

         'SecurityGroup': 'STANDARD',

         'NLSLanguage': 'AMERICAN',

         'Org_Id': '204'

         },

         'InputParameters': {

         'P_REPORT_HEADER_ID': '37345'

         }

         }

         };

        var headers =

        {

         'Accept': 'application/json',

         'Content-Type': 'application/json',

         //'Authorization': 'Basic c3lzYWRtaW46b3JhY2xlMTI=',

        // 'oracle-mobile-backend-id': 'd838ab82-9755-4f54-bca1-38270afac134',

        // 'oracle-mobile-uitooling-backend-name': 'Test',

         //'oracle-mobile-uitooling-backend-version': '1.0',

         'oracle-mobile-api-version':'1.0'

         }

        //var optionList = {

        // inType: 'json',versionToInvoke: '1.0',outType:'json'

        //};

        //req.oracleMobile.connectors.post('ExpenseHeaderDetails', body,optionList).then(

        req.oracleMobile.connectors.ExpenseHeaderDetails.post('ExpenseHeaderDetails', body,

        {externalAuthorization: 'Basic c3lzYWRtaW46b3JhY2xlMTI='}, {headers: headers}).then(

        function (result) {

        res.send(result.statusCode, result.result);

        },

        function (error) {

        console.dir(error);

        res.send(500, error.error);

        }

        );

        });

      };

      Error log : { error: [Error: Argument error, options.body.] }

      Thanks,

      Pallavi.

    • Tom Ware

      Hopefully one minor tweak will do it:

      req.oracleMobile.connectors.ExpenseHeaderDetails.post('ExpenseHeaderDetails', body,

        {externalAuthorization: 'Basic c3lzYWRtaW46b3JhY2xlMTI=', inType: 'json'}, {headers: headers}).then(

    • Pallavi N

      Hi Tware,

      It Worked !!

      Thanks for your support and being patient.

      Pallavi.