URI Parameter validation in MCS/nodejs

混江龙づ霸主 提交于 2019-12-02 06:37:55

问题


Need your help to solve a problem . I am writing a custom API in MCS with method type as "GET" , passing the parameter along with the URI .

service.get('/mobile/custom/****/deviceVersion/:deviceType',function(req,res){
    var reqParams = req.params;
    var finalResponse;
    var params='/'+reqParams.deviceType;
    console.info("Request Params>"+params);
    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{
        //console.info("In Else and length=>"+reqParams.deviceType+"//"+reqParams.deviceType.length);
        //var params='/'+reqParams.deviceType;
        var connector='/deviceVersion';
        commonHandler.CommonHandlerGetMethodFunction(req,res,connector,params);
    }

});

In my case i have to check whether the parameter deviceType is null or not .

I have tried the following methods

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

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

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

Could anyone please tell me the right approach for the validation against null ? Thanks in Advance

Updated

The error i am getting while passing deviceType as null 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/"
 }

Endpoint given in MCS Custom Api is /deviceVersion/{deviceType}


回答1:


This question was cross posted to the Oracle OTN MCS forum and answered there: https://community.oracle.com/thread/4012301




回答2:


if (!req.params.deviceType) should work.



来源:https://stackoverflow.com/questions/41933674/uri-parameter-validation-in-mcs-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!