How to send a nested array through $_GET or $_POST

i need to send a nested array through _GET or _POST. i can’t seem to figure out how to enter the data in the variable section. here’s a screenshot of what i have:

so how do i enter the array elements properly so they are recognized as an array and not a string. because that borks the code that relies on it being an array…

It looks like you are drying to make each array entry and object. I can’t see the whole thing, but I recommend trying {“id”:132421,“reportId”:“2927483”,"dCode:“EP0ee309”} --that would be value JSON for each directive array entry. Hope that helps.

nope. still not working. i don’t know how to make this any clearer. i need a nested array. needs to look like this, because this is what the webapp itself is actually sending through _GET/_POST:

discArray: {
  827422: {
    building_Name: "Palace of Doom",
    directives: {
      0: {
        id: 132421,
        reportID: "2927483",
        dCode: "EP0ss309",
        dInfo: "keep dry and free of water",
        dDue: "2020-05-14",
        ... &c.
      },
      1: {
        id: 132421,
        reportID: "2927483",
        dCode: "EP0ss309",
        dInfo: "keep dry and free of water",
        dDue: "2020-05-14",
        ... &c.
      },
    }
  }
}

so when i made the changes you suggested, i basically got the same thing again: just strings, not nested arrays:

i hope this helps understand what i need a bit better. if not, please let me know. because if i’m stuck using one dimensional arrays, then this whole app is useless… and i doubt that’s the case. so i’m just needing to know how to give what it needs to do the job.

Hi @iansebryk,

With PHP’s parse_str, I think you’ll have to be a bit more verbose when setting the query params.

Name Value
directives[0][id] 132421
directives[0][reportID] 2927483
directives[1][id] 132422
directives[1][reportID] 2927484

EDIT: Alternatively, it might make more sense to add this with a pre-request script to reduce the manual work.

Hope that helps.

Best,

Kevin

“pre-request script”… that’s what i need because the other way is flat out torture. is it something you can illustrate quickly, or do i need to spend an afternoon researching it on the website?

EDIT: found the ‘tutorial’. useless. have no idea what how the hell i’d send an array with it. at least i have a toe-hold. thank you.

@iansebryk,

Here’s an example of how it might look.

const querystring = require('querystring');

const obj = {
    buildingName: "Palace of Doom",
    directives: [
        {
            id: 132421,
            reportID: "2927483",
            dCode: "EP0ss309",
            dInfo: "keep dry and free of water",
            dDue: "2020-05-14"
        },
        {
            id: 132421,
            reportID: "2927483",
            dCode: "EP0ss309",
            dInfo: "keep dry and free of water",
            dDue: "2020-05-14"
        },
    ]
};

const formatted = {
    buildingName: obj.buildingName
}

obj.directives.forEach((d, i) => {
    const topLevel = `directives[${i}]`;
    for (let [key, val] of Object.entries(d)) {
        formatted[`${topLevel}[${key}]`] = val;
    }
});

const qs = querystring.stringify(formatted)
pm.request.addQueryParams(qs);

You can import the collection with this URL: https://www.getpostman.com/collections/e0da4ab4f566c1e65355

Best,

Kevin

whoa. that’s a hell of a lot of code to just import a nested array! thank you for taking time out of your day to do that for me! O_o where did you find the info to build that thing? i couldn’t find anything of depth or use on the website.

The issue is that Node.js doesn’t stringify nested arrays/objects the same way PHP does. So there’s a dozen lines of code or so to do that.

We could lighten the weight of the code a bit, but it requires pulling in another Node.js module, qs. Pulling in external modules can add a layer of complexity, so I try to solve issues with built-in functionality first. (See this template: Browserify CDN modules.)

Here’s what the code would look like with the qs module:

new Function(pm.variables.get('init'))(pm);

const obj = {
    buildingName: "Palace of Doom",
    directives: [
        {
            id: 132421,
            reportID: "2927483",
            dCode: "EP0ss309",
            dInfo: "keep dry and free of water",
            dDue: "2020-05-14"
        },
        {
            id: 132421,
            reportID: "2927483",
            dCode: "EP0ss309",
            dInfo: "keep dry and free of water",
            dDue: "2020-05-14"
        },
    ]
};

const qs = require('qs');
pm.request.addQueryParams(qs.stringify(obj));

Here’s the same collection, except using the qs module: https://www.getpostman.com/collections/367fad795a1296793f23

gotcha. as much as i bitch and moan about this, i do appreciate your help learning this tool. long day and frustration has me tetchy and looking for simple solutions… :stuck_out_tongue: so thanks again for your patient instruction. just when i think i know a tool… oh look. more. :smiley: my biggest concern now, is where to find this information because, as i mentioned earlier, i found none of this on the website.

1 Like