Insomnia: [Feature Request] Advanced Response Filtering by scripting

Created on 25 Oct 2017  路  5Comments  路  Source: Kong/insomnia

image

Script example:

export default function() {
    return resp.map(doc => {
        return {_key: doc._key, position: doc.position};
    })
}

So I can see two specific fields of all items.

Most helpful comment

You can sort of do this already with JSONPath, although the output isn't very pleasent.

Something like $.*["_key","position"] will select both keys from every object in the root array.

All 5 comments

You can sort of do this already with JSONPath, although the output isn't very pleasent.

Something like $.*["_key","position"] will select both keys from every object in the root array.

@gschier Thanks for the skill, I am not that familiar with JSONPath. But do you think script-filter is a more general function and can do more you want.

Script filtering would definitely be more powerful, but it also adds a lot of complexity to the app. In the future, the plugin system should be powerful enough to support such a feature. For now, JSONPath seems to be working for most people 馃槃

In the future, the plugin system should be powerful enough to support such a feature.

Is it the future yet, @gschier?

I ask 'cause I'm working with a response body that's basically

{
    "other": "stuff",
    "items": [
        {
            "image_resources": [
                {"src": "https://cdn.com/small.jpg"},
                {"src": "https://cdn.com/med.jpg"},
                {"src": "https://cdn.com/large.jpg"}
            ],
            "is_video": true,
            "video_resources": [
                {"src": "https://cdn.com/small.mp4"},
                {"src": "https://cdn.com/large.mp4"}
            ]
        },
        {
            "image_resources": [
                {"src": "https://cdn.com/small.jpg"},
                {"src": "https://cdn.com/med.jpg"},
                {"src": "https://cdn.com/large.jpg"}
            ],
            "is_video": false
        }
    ]
}

And from that, I'd like to easily end up with

[
    "https://cdn.com/large.mp4",
    "https://cdn.com/large.jpg"  
]

the result of "if the item is a video, return its large video; otherwise, return its large image".
$.items[?(@.is_video)].video_resources[-1:].src gets me one part of that.
While $.items[?([email protected]_video)].image_resources[-1:].src gets me the other.
I've not found that JSONPath supports the composition of the two expression to produce the result I'd like. So, scripting support, right?

Not yet. I think, ideally, the plugin system would add a new type of plugin for adding custom response filters that could be tied to specific content types.

Was this page helpful?
0 / 5 - 0 ratings