hoverfly icon indicating copy to clipboard operation
hoverfly copied to clipboard

Passing a Parameter dynamically to response from request

Open priyathamreddy6009 opened this issue 9 years ago • 7 comments

Hi Team, We are trying to stub one Json response using hoverfly. Currently we are getting static response for any request. We are expecting to get one value dynamically(empID) in response that should be take from request.

Example: Request: {"employee":{"empName":"Naren","empDesg":Test,"empID":"76443",}}

Response: { "employee": { "empID":"76443" }, "status": "success" } So in the above example the empID value keep changing for all employees. So we want to get the empID dynamic value in response while stubbing through Hoverfly. Can you please help us here.

priyathamreddy6009 avatar Nov 22 '17 08:11 priyathamreddy6009

Can someone help me here?

priyathamreddy6009 avatar Nov 28 '17 08:11 priyathamreddy6009

You can use middleware to do that with Hoverfly in simulate mode. Both the request body and the corresponding response are available to the middleware. There is an example here and there's a SOAP example in the hoverpy documentation here. @tommysitu was showing me a Java example but I can't find that at the moment. Hopefully he can find it and post it.

I think the documentation could be clearer. Some of the diagrams do not show middleware being available in simulate mode.

JohnFDavenport avatar Nov 28 '17 21:11 JohnFDavenport

@priyathamreddy6009 I need to point out that using request body for response templating is not supported yet, so the only way is using middleware as @JohnFDavenport suggested. You can have a look here to see what's available: https://hoverfly.readthedocs.io/en/latest/pages/keyconcepts/templating/templating.html

tommysitu avatar Nov 29 '17 09:11 tommysitu

@priyathamreddy6009 You should be able to use response templating to do this in the next release, your response body should look like this:

"response": {
	"status": 200,
	"body": "{ \"employee\": { \"empID\": \"{{Request.Body \"jsonpath\" \"$.employee.empID\"}}\" }",
        "templated": true
}

tommysitu avatar Jul 20 '18 10:07 tommysitu

@tommysitu I liked the response templating feature you mentioned above. Do you have a link to its documentation?

Teklie avatar Apr 02 '20 12:04 Teklie

@Teklie https://docs.hoverfly.io/en/latest/pages/keyconcepts/templating/templating.html

ns3777k avatar Apr 02 '20 12:04 ns3777k

@Teklie https://docs.hoverfly.io/en/latest/pages/keyconcepts/templating/templating.html

Thanks @ns3777k !

Teklie avatar Apr 02 '20 12:04 Teklie