Response templates are sort of macros used to manipulate the API response to meet the entity needs. It is widely used in the web service world of technologies. There are multiple number of web service mock service tools or providers available in the market to meet the technology requirements.
In this post, we are going to see the data manipulation of server response to mee the client module expectation. Response templating tried for mockoon and mocklab;
Response templating in Mockoon using faker js:
Request:
{
“name_id“: “5ea90ac0d4d18f3212029d15”,
“client”: “5d9d947784062f0ab4097ef4”,
“signAdb”: {
“name”: “sample data”,
“reference_id”: “1234”
},
“latLong”: {
“id“: “5dccd5b576dbcb003650add2”,
“reference_id”: “4567”,
“latitude”: 28.5971716,
“longitude”: 77.1958228
},
“date”: “2020-04-29T10:33:50.000Z”,
“address”: {
“id“: “5dccd476e4ffb1010d8ebd4f”,
“name”: “sample name”
}
}
}
Response:
{
“name_id”: “{{ body name_id ‘ }}”,
“address”: “{{ body latLong.id‘ }}”,
“latLong”: “{{ body address.id‘ }}”,
“statusCode”: 000,
“uniqId”: {{int 10001 99999 round=5}}, // Five digit number has been auto generated
“cFig”: “{{ int 1000000001 9999999999 round=5 }}” // Ten digit number has been auto generated
}
As like Mockoon, we can see mocklab for same sort of template creation as mentioned below. Mocklab is a cloud tool which is used to generate web service mock in public domain instead of local environment and you can able to generate end points including payload processing under the collaborative model.
Please refer the below URL for your quick reference;
Response templating in Mocklab using faker js:
Request:
{
“name_id“: “5ea90ac0d4d18f3212029d15”,
“client”: “5d9d947784062f0ab4097ef4”,
“signAdb”: {
“name”: “sample data”,
“reference_id”: “1234”
},
“latLong”: {
“id“: “5dccd5b576dbcb003650add2”,
“reference_id”: “4567”,
“latitude”: 28.5971716,
“longitude”: 77.1958228
},
“date”: “2020-04-29T10:33:50.000Z”,
“address”: {
“id“: “5dccd476e4ffb1010d8ebd4f”,
“name”: “sample name”
}
}
}
Response:
{
” name _id”: “{{jsonPath request.body ‘$. name _id‘}}”,
” address “: “{{jsonPath request.body ‘$. address.id‘}}”,
” latLong “: “{{jsonPath request.body ‘$. latLong.id‘}}”,
“statusCode”: 000,
” uniqId “: {{randomValue length=5 type=’NUMERIC’}},
” cFig”: “{{randomValue length=10 type=’NUMERIC’}}”
}