Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Janaki Narayanan
    How to create a Connector using REST URL having headers?
    Topic posted March 7, 2017 by Janaki NarayananRed Ribbon: 250+ Points, last edited March 21, 2017 
    297 Views, 14 Comments
    Title:
    How to create a Connector using REST URL having headers?
    Summary:
    How to create a Connector using REST URL having headers?
    Content:

    RestConnector.png

    I have a REST URL(ISG) that has header parameters.How can I create a REST connector using that REST URL in oracle MCS.

    The REST URL is POST method and have 5 header parameters.

    please clarify me below

    1)where I have to attach the headers of rest URL (in rules or during testing endpoint)

    2)If in testing endpoint means I have to add headers in my custom api  using Node js?

    restconnector-Op.png

    Now,I have added headers in Rules,while Testing endpoint it says authentication error so I added headers there also. Now I get 415 error code.

    Comment

     

    • Chris Muir

      Have you read the documentation? https://docs.oracle.com/en/cloud/paas/mobile-cloud/mcsua/rest-connector-apis.html#GUID-12D976D7-5FCA-49C1-8D85-44A9BC438…

      Have you watched the videos? https://www.youtube.com/watch?v=AzRYJ_wQduY&list=PL2ux0DjE-RYf7Rzh05Y98QWroTb76yyov

      If yes can you be more specific on your question on what you don't understand please.

      CM.

    • Chris Muir

      Janaki, is your question about REST Connectors in general in MCS, or specifically about adding HTTP headers to a REST Connector call via MCS?

      If the later, there are 2 ways to do this:

      a) For static non changing HTTP headers use the REST HTTP Header "Rules" feature:

      https://docs.oracle.com/en/cloud/paas/mobile-cloud/mcsua/rest-connector-apis.html#GUID-21EBB7F1-CDD3-4812-925A-607B76A84…

      b) If the HTTP headers are dynamic, instead in your NodeJS code when you call the REST connector:

      service.get('/mobile/custom/incidentreport/weather/:city',

        function (req, res) {

        var body = {

        Header: {

          myHeader1:"myValue1",

          myHeader2:"myValue2"

        },

        Body: {

          GetWeather: {

            CityName: req.params.city,

            CountryName: 'Spain'

          }

        }

      };

      req.oracleMobile.connectors.post('globalweather', 'GetWeather', body).then(

        function (result) {

          console.info("result is: " + result.statusCode);

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

        },

        function (error) {

          console.info("error is: " + error.statusCode); 

          res.send(500, error.error);

        }

        );

        });


      CM.

    • Janaki Narayanan

      I have gone through the documentation and video in youtube ,but i didnt get a clear idea about the REST Connectors that created with Rest URL with headers.

      I also tried the REST API (google api) provided in oracle and it is working fine.Few sample API like weather api is also working fine.

      I have elaborated my question.Please check it out.

      Thanks& Regards,

      -Janaki

    • Tom Ware

      See: Calling MCS APIs from Custom Code

      Note:

      You might notice that you don’t need to worry about authentication when you send requests to MCS APIs from custom code. MCS re-uses the access token that’s passed into the custom code and takes care of authentication for you. With connectors, if you need to use different credentials for the external service, you can use options.externalAuthorization

      to pass the value to be used in the Authorization

      header for the external service.

    • Janaki Narayanan

      Hi Chris,

      I have rest URL with authorization as one of the header parameter.When I Test the Rest Connector I am able to add the authorization as a header parameter and test it successfully.But when I test the custom API created using that connector  I am getting below error.Screenshot.jpg

      I will also attach my Node Js 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.get('/mobile/custom/Login_Rest/login', function(req,res) {

        var body = {

        Header: {

      "content-type": req.header.content-type,

      "authorization": req.header.authorization,

      "Responsibility":  req.header.Responsibility,

      "RespApplication": req.header.RespApplication,

      "SecurityGroup":  req.header.SecurityGroup,

      "NLSLanguage":  req.header.NLSLanguage,

      "Org_Id":  req.header.Org_Id

        },

        Body: {

        username: req.params.username,

      password:req.params.password

        }

      };

      req.oracleMobile.connectors.post('TestRestAPI', '', body).then(

        function (result) {

          console.info("result is: " + result.statusCode);

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

        },

        function (error) {

          console.info("error is: " + error.statusCode);

          res.send(500, error.error);

        }

        );

        });

      };

      Please clarify me below

      How to pass authorization header paramter?

      whether there is any error  with the custom code ?

    • Janaki Narayanan

      Thanks for your response Tware

      Still I didn't create a custom API using REST connectors successfully.

      Can you please verify the steps I follow.

      Entered API name and created resources with GET method.I have given two input parameters as query and Headers parameters as headers.

      My Custom code is

      /**

      * 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.get('/mobile/custom/MobileLogin_Rest/userLogin', function(req,res) {

        var body = {

        Header: {

      "Content-type": req.header.content-type,

      "authorization": req.header.authorization,

      "Responsibility":  req.header.Responsibility,

      "RespApplication": req.header.RespApplication,

      "SecurityGroup":  req.header.SecurityGroup,

      "NLSLanguage":  req.header.NLSLanguage,

      "Org_Id":  req.header.Org_Id

        },

        Body: {

            P_USRNAME: req.params.P_USRNAME,

          P_PASSWORD: req.params.P_PASSWORD

        }

      };

      req.oracleMobile.connectors.post('TestRestAPI', '', body).then(

        function (result) {

      console.info("result is: " + result.statusCode);

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

      },

        function (error) {

          console.info("error is: " + error.statusCode);

          res.send(500, error.error);

        }

        );

        });

      };

      And also clarify me .

      Whether I have to change the authorization to options.externalAuthorization in Headers parameters?

    • Tom Ware

      Your code should look roughly like this:

      service.get('/mobile/custom/MobileLogin_Rest/userLogin', function(req,res) {

        var headers = {

      "Content-type": req.header.content-type,

      "Responsibility":  req.header.Responsibility,

      "RespApplication": req.header.RespApplication,

      "SecurityGroup":  req.header.SecurityGroup,

      "NLSLanguage":  req.header.NLSLanguage,

      "Org_Id":  req.header.Org_Id

        };

      var body =  {

            P_USRNAME: req.params.P_USRNAME,

          P_PASSWORD: req.params.P_PASSWORD

        };

      req.oracleMobile.connectors.post('TestRestAPI', '', body, {inType: 'json', externalAuthorization: <authorization for your external service here if needed> }, {headers: headers}).then(

        function (result) {

      console.info("result is: " + result.statusCode);

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

      },

        function (error) {

          console.info("error is: " + error.statusCode);

          res.send(500, error.error);

        }

        );

        });

      };

    • Janaki Narayanan

      Hi Tware,

      Now getting below error

      Thanks,Screenshot001.jpg

    • Tom Ware

      Take a look at your logs.  There should be a log message with a stack trace.  That will be useful in tracking this down.

      If you don't see that message, please share your identity domain name.

    • Janaki Narayanan

      I found that error in log in that log.Identity Domain is gse00010158.

    • Tom Ware

      What is the error?  Please include the stack trace.

    • Janaki Narayanan

      Hi Tware,

      Below is the stack trace .Please help me.

      Message Text

      Custom Code Problem: request: get /mobile/custom/TransRestApi/Username caused unhandled error: ReferenceError: type is not defined at /mobile/mobile_ccc/custom_code_modules/transrestapi_1.0.0_7/transrestapi/transrestapi.js:25:36 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) at Object.methodOverride [as handle] (/mobile/mobile_ccc/mcs-node-router/node_modules/method-override/index.js:63:14) at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:193:15) at Object.urlencoded [as handle] (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:40:37) at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:193:15) at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:195:9) at Object.json [as handle] (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/middleware/json.js:42:37) at next (/mobile/mobile_ccc/mcs-node-router/node_modules/express/node_modules/connect/lib/proto.js:193:15)

    • Tom Ware

      The top line of the stack trace tells you where the problem is:

      type is not defined at /mobile/mobile_ccc/custom_code_modules/transrestapi_1.0.0_7/transrestapi/transrestapi.js:25:36

      Look on line 25 of transrestapi.js.  Do you see "type" there.  It need to be defined before you can use it.

    • Abhishek Singh

      I am facing similar issue with connector calling, tried two approaches, no success

      Tried two approached, failed in both

      1.

          req.oracleMobile.connectors.cxproductsconnector.post(   
      '/login', // resource
               { externalAuthorization: req.header('external-authorization')},
              
           { qs: {},
           headers: { "accept": req.header('accept'),
           "Content-Type": req.header('application/x-www-form-urlencoded'),
                             "Authorization": req.header('Bearer '+appkey),
                            "grant_type": req.header('client_credentials')
                           }
           }

       

      2.

        var appkey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eytYjA3ZCc3MiOiJhcHBsaWNhdGlvbkF1dGgiLCJleHAiOjE1NjI4MzcwMTUsImlhdCI6MTUzMTMwMTAxNX0=.Qh4nwp51z824xCxEj9A0gSkwM2Xf1T2+/8j3QG1hWLw=';
       
        var headers= {
                        'Content-Type': 'application/x-www-form-urlencoded',
                          'Authorization': 'Bearer '+appkey,
                      };
       
      var body = {
                    'grant_type' : 'client_credentials'
                    };
        service.post('/mobile/custom/tst_cxproducts/products', function(req,res) {
       
          req.oracleMobile.connectors.cxproductsconnector.post(
            '/login',
             body,
             {inType: 'json', externalAuthorization: 'Bearer '+appkey },
           {qs : {}, headers: headers}