Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Anand Raj
    How to validate path variable/Uri parameter
    Topic posted January 31, 2017 by Anand RajRed Ribbon: 250+ Points, last edited February 17, 2017 
    94 Views, 8 Comments
    Title:
    How to validate path variable/Uri parameter
    Summary:
    How to validate path variable/Uri parameter
    Content:

    Hi ,
    I have a Custom Api with method as GET , passing the parameter along with the url . Could you please suggest a method to check whether the parameter is null or not ?

    Below given my code .

    service.get('/mobile/custom/****/deviceVersion/:deviceType',function(req,res){
                
    var reqParams = req.params;
                
    var finalResponse;
                
    var params='/'+reqParams.deviceType;
                
    if(reqParams.deviceType=='{}'){   // ***Here is my problem***
                     finalResponse
    = jbuilder.encode(function (json) {
                          json
    .set('Response', function (json) {
                          json
    .set('responseCode', '400');
                          json
    .set('responseMessage', 'Malformed request query');
                     
    });
               
    });
                res
    .status(400).send(finalResponse);
                res
    .end();
               
    }else{
            var connector='/deviceVersion';
           commonHandler
    .CommonHandlerGetMethodFunction(req,res,connector,params);
      
    }

    });

    I have tried the following

    1. if(reqParams.deviceType=='{}'){}

    2. if(JSON.Stringify(reqParams.deviceType)=='{}'){}

    3. if(JSON.Stringify(reqParams.deviceType).length==0){}

    Also is there anyway to debug the code other than using console?

    The error that i am getting is

    {
      
    "type": "w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
      
    "status": 404,
      
    "title": "API not found",
      
    "detail": "We cannot find the API ******/1.0 for the provided URL path /deviceVersion/. Verify your request parameters and try again.",
      
    "o:ecid": "005Hp2YhoPF3j4C_nDs1yZ000Uba00001w, 0:3",
      
    "o:errorCode": "MOBILE-57945",
      
    "o:errorPath": "/mobile/custom/******/deviceVersion/"
    }

    ​ , could you please help me with this ?

    Comment

     

    • Chris Muir

      I assume the 6 stars (******) in your examples is where you have deliberately obfuscated your custom API name for some reason.

      In considering the parameter I'm assuming deviceType is a string?  As such have you tried the following to check if the parameter String is null?:

      if (reqParams.deviceType == null || reqParams.deviceType === "")

      As this is 101 JavaScript syntax I can only assume you've tried this and it didn't work for you.  Can you elaborate what your testing for the above statement returned please?

      As for the error code you're receiving, I'm guessing that is raised by your call to commonHandler.CommonHandlerGetMethodFunction as your current if statement I'm guessing is failing the test deviceType == '{}' and failing through to the else statement.  I think that error is a red herring to what you're trying to solve in this forum thread.

      CM.

    • Anand Raj

      Thanks for your time and reply .

      • As you mentioned above I have tried using if (reqParams.deviceType == null || reqParams.deviceType === "") . But I am getting the same error that i mentioned above .
      • The "deviceType" parameter is String.
      • ( As for the error code you're receiving, I'm guessing that is raised by your call to commonHandler.CommonHandlerGetMethodFunction as your current if statement I'm guessing is failing the test deviceType == '{}' and failing through to the else statement.  I think that error is a red herring to what you're trying to solve in this forum thread.)

               Reply for your above point . When i comment (//) the "else" statement i am also getting the same error . when i invoke api without the parameter deviceType , it shows that there is no such Url /mobile/custom/******/deviceVersion/ . I think MCS expects a value after deviceVersion/<> .

    • Chris Muir

      Right, I understand.  So you're calling the following URL endpoint via a GET:

      {baseURL}/mobile/custom/****/deviceVersion

      ...without a further value such as:

      {baseURL}/mobile/custom/****/deviceVersion/blah

      Why Express is complaining is you haven't defined a handler for the former URL pattern (or at least you haven't copied the code into this post).  As such your code within the handler is never getting called.

      If you are going to raise queries that hit URLs at the former endpoint, you need to also define that endpoint in the MCS custom API designer.


      CM.

    • Anand Raj

      Sorry to say that i didn't get what you said .

      Endpoint defined in MCS is deviceVersion/{deviceType}

      I am getting right response when i pass the deviceType along the Url . Issue comes only if when we invoke the url  without deviceType

    • Chris Muir

      When you build MCS custom APIs, you both build their endpoints in the design section, and then you write code in NodeJS for each endpoint.

      So in the designer you might build endpoints like:

      GET /custom/mobile/blah/x

      GET /custom/mobile/blah/y

      GET /custom/mobile/blah/y/someValue

      When you generate and modify the NodeJS scaffold code for the first time, you should have code similar to:

      #1 service.get('/custom/mobile/blah/x', function(req, res) { etc }

      #2 service.get('/custom/mobile/blah/y', function(req, res) { etc }

      #3 service.get('/custom/mobile/blah/y/:someParam', function(req, res) { etc }

      As such if you invoke GET /custom/mobile/blah/x #1 will be called.

      If you invoke GET /custom/mobile/blah/y #2 will be called

      If you invoke GET /custom/mobile/blah/y/someValue) #3 will be called.

      **Note** for the last one, if you omit the parameter value someValue, #2 will be called.

      In fact Express expects #2 to be defined in your list of functions.  It will not pass the call to #3 with deviceType == null.

      This is where you've gone wrong.  Using your example when you're calling:

      {baseURL}/mobile/custom/****/deviceVersion

      ...you're expecting the following to be called:

      {baseURL}/mobile/custom/****/deviceVersion/:deviceType

      ...but Express is looking for the following:

      service.get('/mobile/custom/****/deviceVersion', etc)

      In other words, the fact you haven't supplied a deviceType doesn't mean the following is called with deviceType = null:

      service.get('/mobile/custom/****/deviceVersion/:deviceType', etc)

      ...rather Express is looking for the following instead:

      service.get('/mobile/custom/****/deviceVersion', etc)

      As a recommendation, I suggest you review the NodeJS Express documentation on "Routers" to understand how this all works:

      https://expressjs.com/en/guide/routing.html

      CM.

    • Anand Raj

      Thank you . Now i understood .

    • Anand Raj

      @Chris Muir-Oracle

      Just want to know , is there any way to debug the code other than using console?

    • Chris Muir

      There is no current debugger.  Your best solution at this time is to include console.log calls and you can view them in the MCS diagnostics.

      CM.