kahmali / meteor-restivus

REST APIs for the Best of Us! - A Meteor 0.9+ package for building REST APIs https://atmospherejs.com/nimble/restivus
MIT License
544 stars 117 forks source link

Post array of json objects? Possible and how? #285

Closed anthonymanzo closed 6 years ago

anthonymanzo commented 6 years ago

Hi, So far I've traveled down the path of raw body and as a query parameter encoded value, but none of that really works right (json gets escaped).

Here's an example3 of what I'm trying to post:

[{  "_id" : "db551cc9e3092dea5ba201b2",     "associated_request" : "PPMS2-BPHO-20100822 REF#50",    "fund_code" : "599608",     "requestor_name" : "Zhu Quan",  "group" : "verma",  "charge_date" : "2010/08/13",   "charge_description" : "Zeiss LSM 710",     "charge_amount" : "20.00",  "charge_minutes" : "120",   "charge_start_time" : "17:00",  "recharge_id" : "bCufiGpNLm4r3aRpL",    "dept_code" : "LOG-V",  "org" : "Salk",     "createdAt" : ISODate("2017-08-10T03:00:45.581Z"),  "createdBy" : null,     "modifiedAt" : ISODate("2017-08-10T03:00:45.581Z"),     "modifiedBy" : null } {     "_id" : "51ca6bb041a142923e681323",     "associated_request" : "PPMS2-BPHO-20100722 REF#1",     "fund_code" : "620043",     "requestor_name" : "Kagalwala Mohamedi",    "group" : "gage",   "charge_date" : "2010/07/20",   "charge_description" : "Zeiss LSM 710",     "charge_amount" : "40.00",  "charge_minutes" : "120",   "charge_start_time" : "10:00",  "recharge_id" : "ctqfmFi6iBHzZy48S",    "dept_code" : "LOG-G",  "org" : "Salk",     "createdAt" : ISODate("2017-08-10T03:00:47.974Z"),  "createdBy" : null,     "modifiedAt" : ISODate("2017-08-10T03:00:47.974Z"),     "modifiedBy" : null } {     "_id" : "ebd19cceb8c4dd2aae7f83af",     "associated_request" : "PPMS2-BPHO-20100722 REF#2",     "fund_code" : "620043",     "requestor_name" : "Kagalwala Mohamedi",    "group" : "gage",   "charge_date" : "2010/07/20",   "charge_description" : "Zeiss LSM 710",     "charge_amount" : "5.00",   "charge_minutes" : "240",   "charge_start_time" : "13:00",  "recharge_id" : "ctqfmFi6iBHzZy48S",    "dept_code" : "LOG-G",  "org" : "Salk",     "createdAt" : ISODate("2017-08-10T03:00:48.040Z"),  "createdBy" : null,     "modifiedAt" : ISODate("2017-08-10T03:00:48.040Z"),     "modifiedBy" : null } {     "_id" : "16fc89ee8f005cd89dfdd65f",     "associated_request" : "PPMS2-BPHO-20100722 REF#10",    "fund_code" : "620043",     "requestor_name" : "Kagalwala Mohamedi",    "group" : "gage",   "charge_date" : "2010/07/20",   "charge_description" : "Zeiss LSM 710",     "charge_amount" : "60.00",  "charge_minutes" : "180",   "charge_start_time" : "14:00",  "recharge_id" : "ctqfmFi6iBHzZy48S",    "dept_code" : "LOG-G",  "org" : "Salk",     "createdAt" : ISODate("2017-08-10T03:00:48.130Z"),  "createdBy" : null,     "modifiedAt" : ISODate("2017-08-10T03:00:48.130Z"),     "modifiedBy" : null }]

How would I setup my custom endpoint so I can get ahold of the data as a json array?

Api.addRoute('recharges/insert-recharge-items-and-post-recharge',{authRequired: false},{
    post:function(){

        console.log('how can i see/work with the array of json in the raw body here?');

        return {status: 'success'};
    }
});

Many Thanks!

anthonymanzo commented 6 years ago

Hi, So I found that the JSON parser was picky in seeing tabs and new lines. So first I removed those from my Post body. But I was still having problems with the built-in JSON.parse throwing errors for unexpected end of input and whatnot. Here was my final solution (I used npm json-parse ):

Api.addRoute('recharges/insert-recharge-items-and-post',{authRequired:false},{

    post:function(){

        let data = this.bodyParams // parsed params 

        let urlParams = this.queryParams; // parsed get quries

        let readable = this.request; 

        let arrStr =readable._readableState.buffer.toString('utf8');

        arrStr = parseJson(arrStr);

        return {status: 'success', data: arrStr};

    }
});
anthonymanzo commented 6 years ago

And somewhere in the code before this Api route:

const parseJson = require('parse-json');