When defining base and sub environments you're unable to dump a raw variable to return a JSON object.
For example, the following environment results in an Unexpected token { in JSON at position 286 error:
{
"addresses": {
"home": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
},
"work": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
}
},
"people": {
"name": "John Pinkerton",
"phone_number": "123-456-7890",
"address": {{ addresses.home | dump }}
}
}
The problem is specific to the {{ addresses.home | dump }} variable. I don't get the error if I surround the variable in quotes but then it is treated as a string instead of an object. The same variable works fine in a request body.
EDIT: If it wasn't obvious the expected behavior is that {{ addresses.home | dump }} would parse out to the full JSON object:
{
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
}
馃憢 Thanks for opening your first issue! If you're reporting a 馃悶 bug, please make sure
you include steps to reproduce it. If you're requesting a feature 馃巵, please provide real
use cases that would benefit. 馃應
To help make this a smooth process, please be sure you have first read the
contributing guidelines.
This request totally makes sense and would be super useful indeed!
The reason that it doesn't work currently is that the environment is stored as a JSON object on the database model and request bodies are stored as raw strings. This means that invalid JSON in an environment will not save but will in a request body. Making this work is pretty tricky since it involves changing the way environments are stored. However, there may be another solution.
What about a template tag that could pull results out of a JSON string instead? Your example would become a bit more verbose, as seen here:
{
"addresses": {
"home": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
},
"work": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
}
},
"people": {
"name": "John Pinkerton",
"phone_number": "123-456-7890",
"addressStr": "{{ addresses.home | dump }}",
"address": {
"line1": "{% jsonpath people.addressStr, '$.line1' %}",
"city": "{% jsonpath people.addressStr, '$.city' %}",
"state": "{% jsonpath people.addressStr, '$.state' %}",
"zip_code": "{% jsonpath people.addressStr, '$.zip_code' %}"
}
}
}
I know that you're desired solution is obviously better, but my proposed alternative could be added as a simple plugin today. In fact, I already wrote a the plugin (haven't published it yet) to see if it would work.
FYI, I Just published the plugin under insomnia-plugin-jsonpath if you want to try it out.
Isn't this already possible by wrapping the variables in quotes?
{
"addresses": {
"home": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
},
"work": {
"line1": "123 E Fake Street",
"city": "Nowheresville",
"state": "AL",
"zip_code": "12345"
}
},
"people": {
"name": "John Pinkerton",
"phone_number": "123-456-7890",
"address": {
"line1": "{{ addresses.home.line1 }}",
"city": "{{ addresses.home.city }}",
"state": "{{ addresses.home.state }}",
"zip_code": "{{ addresses.home.zip_code }}"
}
}
}
As far as I can tell the only advantage the jsonpath string parsing method has is that you would only need to update one variable on the person object to change the address.
EDIT: I guess I was mistaken. I tried my example above and it didn't work. I'll give that plugin a shot.
@PHLAK actually, I didn't even consider that. I must have been too tired 馃槤
I tried your example and it _does_ work for me. If you've placed it in a sub environment, make sure that environment is activated first.
The main benefit of the example I provided is that you only have to change it in one place if you want to use a different address.
Oh yeah, it does work in sub-environments. However, I wanted to have some variables in the base environment and some commonly used requests composed of various combinations of those variables. Then me and my team could easily compose our requests by using the full request variables or a combination of the other variables when those pre-composed requests didn't work. I hope that makes sense.
@PHLAK I think I might need an example to clarify what you're describing.
This is a bit of a contrived example but should hopefully illustrate my desires:
Base Environment
{
"menu_items": {
"cheeseburger": {
"calories": 425,
"price": 299
},
"chicken_nuggets": {
"calories": 350,
"price": 249
},
"french_fries": {
"calories": 300,
"price": 199
},
"fountain_drink": {
"calories": 180,
"price": 150
},
"milkshake": {
"calories": 350,
"price": 250
}
},
"combos": {
"cheeseburger_combo": {
{{ menu_items.cheeseburger | dump }},
{{ menu_items.french_fries | dump}},
{{ menu_items.fountain_drink | dump}}
},
"chicken_nuggets_combo": {
{{ menu_items.chicken_nuggets | dump }},
{{ menu_items.french_fries | dump}},
{{ menu_items.fountain_drink | dump}}
}
}
}
Request Examples
{
"cashier": "John Pinkerton",
"order": {{ combos.cheeseburger_combo | dump }},
"time": "2018-03-08T17:16:49+00:00Z"
}
{
"cashier": "John Pinkerton",
"order": {
{{ menu_items.cheeseburger | dump }},
{{ menu_items.milkshake | dump}}
},
"time": "2018-03-08T17:16:49+00:00Z"
}
I see. Thanks for the example 馃槃
At this moment, plugins aren't able to modify the environment (only read it). But, if this functionality were added, you could write a simple plugin that looked at your environment and built these objects for you.
For example:
"combos": {
"cheeseburger_combo": ["cheeseburger", "french_fries", "fountain_drink"]
}
Then, a plugin could look through the array and replace the cheeseburger_combo array with the full-constructed object before the request is made.
Just an idea for the future.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.