apimocker
apimocker copied to clipboard
Dynamic content in response
Suppose I want dynamic content in reponse json/xml, like time-stamp, random number, uuid, then there should be some provision with enableTemplate feature to serve dynamic content if we annotate our json/xml like:
<queen> <suit>@name</suit> <time>@timestamp</time> <setup>@uuid</setup> </queen>
Hi, that's not a bad idea. But can't you use the existing template feature, or switch feature? Those allowed w you to return dynamic content based on request parameters.
That will make the request a little complex, suppose we want timestamp or random numbers to be generated in response to the same request, then it's a problem. We can have the implementation to replace some common wildcards like uuid, time stamp etc in the fillTemplate function. I can fork this project and implement what I am trying to say, if you like that feature, you can approve my pull request and merge onto master.
Sorry, I didn't get back to you on this. I think your idea is pretty good. I just want to keep it pretty simple if possible. The project is getting more and more complex, so I want to be careful what we are adding. Timestamp and random number make sense to me, although we have to be think about time stamp formats. Not sure about UUID, what would you return in that case? Please do make a fork, and I would be happy to review any pull request. Thanks.
For uuid I'd be extending the uuid node package and will be replacing the wildcard @uuid with generated uuid, it will be more or less like a random number.
At the moment, you can only template the url, but it would be very useful for instance to be able to use the values provided in the querystring in the response body.
Example:
Request
http://myserverorg/tasks?userId=2
Response file
{ "name": "task1", "userId": @query.userId }
@apichick this should be supported now in the latest version. Values can now be inserted into the response body. Let me know how that works for you...
From the docs, I see how to insert values of the request body in the response body, but I don't see how to do it with querystring parameter values in the response body, as in the example I gave.
Template feature supported limited request substitution. The new version with template switch feature supports broader request parameter substitution including GET parameter as well as POST (body) parameter substitution (into a template response).