You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
``addToArray`` will create a new array if one doesn't exist. The boolean argument in ``putValue`` and ``addToArray``
538
541
is used to control whether the set value is returned.
539
542
543
+
``initArray`` will clear an existing array (set it to empty) or create a new empty array if it does not exist. This is useful for resetting arrays inside loops or before reusing them in a template.
544
+
540
545
.. note::
541
546
542
547
Each templating session has its own key value store, which means all the data you set will be cleared after the current response is rendered.
@@ -601,7 +606,7 @@ You can use the following helper methods to join, split or replace string values
@@ -654,6 +659,51 @@ To learn about more advanced templating functionality, such as looping and condi
654
659
655
660
Global Literals and Variables
656
661
-----------------------------
662
+
Setting properties on the response
663
+
----------------------------------
664
+
665
+
Hoverfly provides helper functions to set properties on the HTTP response directly from your templates. This allows you to dynamically control the status code and headers based on request data or logic in your template.
666
+
667
+
Setting the Status Code
668
+
~~~~~~~~~~~~~~~~~~~~~~~
669
+
You can set the HTTP status code of the response using the ``setStatusCode`` helper. This is useful for conditional logic, such as returning a 404 if a resource is not found, or a 200 if an operation succeeds.
670
+
671
+
.. code:: handlebars
672
+
673
+
{{ setStatusCode 404 }}
674
+
675
+
You can use this helper inside conditional blocks:
If you provide an invalid status code (e.g., outside the range 100-599), it will be ignored.
688
+
689
+
Setting Response Headers
690
+
~~~~~~~~~~~~~~~~~~~~~~~~
691
+
You can set or override HTTP response headers using the ``setHeader`` helper. This is useful for adding custom headers, controlling caching, or setting content types dynamically.
692
+
693
+
.. code:: handlebars
694
+
695
+
{{ setHeader "X-Custom-Header" "HeaderValue" }}
696
+
697
+
You can use this helper multiple times to set different headers, or inside conditional blocks to set headers based on logic:
698
+
699
+
.. code:: handlebars
700
+
701
+
{{ setHeader "Content-Type" "application/json" }}
702
+
{{ setHeader "X-Request-Id" (randomUuid) }}
703
+
704
+
If the header already exists, it will be overwritten with the new value.
705
+
706
+
Both helpers do not output anything to the template result; they only affect the response properties.
657
707
You can define global literals and variables for templated response. This comes in handy when you
658
708
have a lot of templated responses that share the same constant values or helper methods.
0 commit comments