Custom code parsing issue- Request Entity Too Large - Oracle MCS

553 views Asked by At

Hi I am facing an issue while accessing custom api using Oracle MCS

{
 "message": "Custom Code Problem parsing JSON: request entity too large"
}

This occur while calling an api with request containing a byte array of an image. my code given below

service.post('/mobile/custom/meraasmcs/documentofrecords/updateemiratesid',function(req,res){
            console.info("Request Before Api call=>"+req);
            var connector = '/documentofrecords/updateemiratesid'
            commonHandler.CommonHandlerPostMethodFunction(
                    req,res,connector)

commonHandler.js

module.exports = {
 CommonHandlerPostMethodFunction: 
     function (req, res, connector) {
        /*
         * Description : This function handles all the request and response
         * Output :     Returns a json content success message
         * Author : Anand Raj
         * Created Date : 10/10/2016
         */
         var app = express();
         var newReqBody=app.use(bodyParser.json({limit: '50mb'}));
         console.info('New ReqBody=>'+newReqBody);
        var errorMsg;
        var finalResponse;
        var headerIndex = JSON.stringify(
                req.headers).search('application/json');
        var versionNumberIndex = JSON.stringify(
                req.headers).search('x-api-version');
        var reqBody = JSON.stringify(req.body);
        console.info('header ' + JSON.stringify(req.headers));
        console.info('version index ' + versionNumberIndex);
        console.info('version number ' + req.headers['x-api-version']);
        console.info('Req body ' + reqBody);
        res.header('Access-Control-Allow-Origin','*');  // To overcome cross origin issue in Windows platform
        if (0 > headerIndex) {
                finalResponse = jbuilder.encode(function (json) {
                     json.set('Response', function (json) {
                          json.set('responseCode', '415');
                          json.set('responseMessage', 'Unsupported media-type');
                     });

                });
            res.status(415).send(finalResponse);
            res.end();
        } else if (reqBody === '{}') {
            finalResponse = jbuilder.encode(function (json) {
                json.set('Response', function (json) {
                  json.set('responseCode', '400');
                  json.set('responseMessage', 'Malformed request body');
                });

            });
            res.status(400).send(finalResponse);
            res.end();
        } else if (0 > versionNumberIndex) {
            finalResponse = jbuilder.encode(function (json) {
                 json.set('Response', function (json) {
                      json.set('responseCode', '400');
                      json.set('responseMessage', 'Version Number missing');
                 });

            });
            res.status(400).send(finalResponse);
            res.end();
        } else {
            /* request part */
            var versionNumber = req.headers['x-api-version'];
            var optionsList = {
                uri: '/mobile/connector/MeraasAllConnector'+connector,
                body: reqBody,
                headers: {
                    'Content-Type': 'application/json',
                    'X-API-Version': versionNumber
                }
            };
            /* handler part */
            var handler = function (error, response, body) {
                if (error) {
                    if ('message' in error) {
                        errorMsg = error.message;
                        //res.send(500, errorMsg);
                        //res.end();
                        finalResponse = jbuilder.encode(function (json) {
                                json.set('Response', function (json) {
                                    json.set('responseCode', '500');
                                    json.set('responseMessage', errorMsg);
                                });

                        });
                        res.status(500).send(finalResponse);
                        res.end();
                    } else {
                        finalResponse = jbuilder.encode(function (json) {
                                json.set('Response', function (json) {
                                    json.set('responseCode', '408');
                                    json.set('responseMessage', 'Response Timed Out');
                                });

                        });
                        res.status(408).send(finalResponse);
                        res.end();
                    }
                } else {
                    if (response.statusCode !== 200) {
                        finalResponse = jbuilder.encode(function (json) {
                                json.set('Response', function (json) {
                                    json.set('responseCode', response.statusCode);
                                    json.set('responseMessage', response.message);
                                });

                        });
                        res.status(response.statusCode).send(finalResponse);
                        res.end();
                    } else if(response.statusCode==401){
                        finalResponse = jbuilder.encode(function (json) {
                                json.set('Response', function (json) {
                                    json.set('responseCode', '401');
                                    json.set('responseMessage', 'Authorization Required.');
                                });

                        });

                         res.status(401).send(finalResponse);
                         res.end();
                    }
                    else {
                        res.status(200).send(body);
                        res.end();
                    }
                }
            };
          req.oracleMobile.rest.post(optionsList, handler);
       }        
}

};

Package.json

    {
  "name" : "meraasmcs",
  "version" : "1.47.0",
  "description" : "Contains all the custom Apis for Meraas Mobile App",
  "main" : "meraasmcs.js",
  "dependencies":
    {
    "jbuilder":"0.0.4",
    "body-parser":"1.15.2",
    "express":"4.14.0"
    },
  "oracleMobile" : {
    "dependencies" : {
      "apis" : { },
      "connectors" : {"/mobile/connector/MeraasAllConnector":"1.0"}
    }
  }
}

If i reduce the size of image then pass its corresponding byte array, then there response will be success.

I checked related questions, all it says

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

But i tried that also. Could anyone please help me??

Sample Request

{
"P_PERSON_ID": 31441,
"P_LOGIN_PERSON_ID": 31441,
"TIME_STAMP": null,
"P_EMIRATES_ID_TAB": [{
    "DocumentExtraInfoId": "",
    "ValidFrom": "2016-11-23",
    "ValidTo": "2016-11-24",
    "CardNumber": "Ggv4"
}],
"P_ATTACHMENT_TAB": [{
    "Title": "image.jpg",
    "DocumentId": null,
    "DeleteFlag": "N",
    "Description": "Testqwe",
    "AttachmentName": "Test",
    "FileData": "",
    "AttachmentType": "image/jpeg"
}]

}

Issue comes when the length of the value of FileData is too large.

Thanks in advance

0

There are 0 answers