Hi Guys .. during response template how i can che...
# help
d
Hi Guys .. during response template how i can check if request json contains a key and if available get the val and return back in the response else return 0 or something else. Something as below however when request has no query.offset then i am returning a blank val which crash the app
Copy code
"offset": {{request.query.offset}},
l
Do you mean your request is a json payload that has a structure like:
Copy code
{
  "query": {
    "offset": 1
  }
}
If the above is a correct assumption then you can use the
jsonPath
helper to pull the value out of the request. The nice thing is that the
jsonPath
helper allows you to specify a default. So, create your response along the lines of:
Copy code
{
    "myOffset": {{jsonPath request.body '$.query.offset' default='0'}}
}
If you then send a request like this:
Copy code
{
  "query": {
    "offset": 4
  }
}
You will get a response like this:
Copy code
{
  "myOffset": 4
}
If you send a request like this:
Copy code
{
  "query": {
    "foo": "bar"
  }
}
You will get a response like this:
Copy code
{
  "myOffset": 0
}
d
Thanks for the tip .. sorted out
👍 1