apimocker icon indicating copy to clipboard operation
apimocker copied to clipboard

Dynamic content in response

Open abhijeetahuja opened this issue 8 years ago • 8 comments

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>

abhijeetahuja avatar May 05 '16 07:05 abhijeetahuja

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.

gstroup avatar May 07 '16 00:05 gstroup

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.

abhijeetahuja avatar May 07 '16 08:05 abhijeetahuja

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.

gstroup avatar May 14 '16 00:05 gstroup

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.

abhijeetahuja avatar May 14 '16 00:05 abhijeetahuja

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 avatar Jun 27 '16 15:06 apichick

@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...

gstroup avatar Jul 22 '16 06:07 gstroup

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.

apichick avatar Jul 24 '16 17:07 apichick

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).

ferrerod avatar Jul 24 '16 20:07 ferrerod