Skip to content

Commit

Permalink
Edit README.md
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Ludes <[email protected]>
  • Loading branch information
deerbone committed Oct 15, 2024
1 parent 94875bb commit 2fa497a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ In the following example, we have defined multiple imposters for the `POST /goph
````

#### Using Templating in Responses
Killgrave supports templating in responses, allowing you to create dynamic responses based on request data. This feature uses Go's text/template package to render templates.
Killgrave supports templating in responses, allowing you to create dynamic responses based on request data. This feature uses Go's `text/template` package to render templates.

In the following example, we define an imposter for the `GET /gophers/{id}` endpoint. The response body uses templating to include the id from the request URL.

Expand All @@ -587,34 +587,35 @@ In this example:
- The endpoint field uses a path parameter {id}.
- The body field in the response uses a template to include the id from the request URL.

You can also use other parts of the request in your templates, such as headers and query parameters.
You can also use other parts of the request in your templates, such query parameters and the request body.
Since query parameters can be used more than once, they are stored in an array and you can access them by index or use the `stringsJoin` function to concatenate them.

Here is an example that includes a query parameter gopherColor in the response:
Here is an example that includes query parameters gopherColor and gopherAge in the response, one of which can be used more than once:

````json
// expects a request to, for example, GET /gophers/bca49e8a-82dd-4c5d-b886-13a6ceb3744b?gopherColor=Blue&gopherColor=Purple&gopherAge=42
[
{
"request": {
"method": "GET",
"endpoint": "/gophers/{id}",
"params": {
"gopherColor": "{v:[a-z]+}",
"age": "{v:[0-9]+}"
"gopherAge": "{v:[0-9]+}"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"id\": \"{{ .PathParams.id }}\", \"color\": \"{{ stringsJoin .QueryParams.gopherColor "," }}\", \"age\": {{ index .QueryParams.age 0 }}}"
"body": "{\"id\": \"{{ .PathParams.id }}\", \"color\": \"{{ stringsJoin .QueryParams.gopherColor "," }}\", \"age\": {{ index .QueryParams.gopherAge 0 }}}"
}
}
]
````

Templates can also include data from the request, allowing you to create dynamic responses based on the request data. Currently only JSON bodies are supported. They also need to have the correct content type set.
Templates can also include data from the request body, allowing you to create dynamic responses based on the request data. Currently only JSON bodies are supported. The request also needs to have the correct content type set (Content-Type: application/json).

Here is an example that includes the request body in the response:

Expand All @@ -631,7 +632,7 @@ Here is an example that includes the request body in the response:
},
"params": {
"gopherColor": "{v:[a-z]+}",
"age": "{v:[0-9]+}"
"gopherAge": "{v:[0-9]+}"
}
},
"response": {
Expand All @@ -653,13 +654,13 @@ Here is an example that includes the request body in the response:
"attributes": {
"name": "{{ .RequestBody.data.attributes.name }}",
"color": "{{ stringsJoin .QueryParams.gopherColor "," }}",
"age": {{ index .QueryParams.age 0 }}
"age": {{ index .QueryParams.gopherAge 0 }}
}
}
}
````
````json
// request body to POST /gophers/bca49e8a-82dd-4c5d-b886-13a6ceb3744b?gopherColor=Blue&gopherColor=Purple&age=42
// request body to POST /gophers/bca49e8a-82dd-4c5d-b886-13a6ceb3744b?gopherColor=Blue&gopherColor=Purple&gopherAge=42
{
"data": {
"type": "gophers",
Expand Down

0 comments on commit 2fa497a

Please sign in to comment.