Skip to content

Commit 203363a

Browse files
NPA-4284: Add mirror request headers in response script
1 parent 828f33a commit 203363a

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<Script async="false" continueOnError="true" enabled="true" name="MirrorRequestHeaders">
3+
<DisplayName>MirrorRequestHeaders</DisplayName>
4+
<Properties/>
5+
<ResourceURL>py://mirror-request-headers.py</ResourceURL>
6+
</Script>

proxies/live/apiproxy/policies/TranslateRequestHeaders.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<Script async="false" continueOnError="false" enabled="true" name="TranslateRequestHeaders">
2+
<Script async="false" continueOnError="true" enabled="true" name="TranslateRequestHeaders">
33
<DisplayName>TranslateRequestHeaders</DisplayName>
44
<Properties/>
55
<ResourceURL>py://translate-request-headers.py</ResourceURL>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Access original request headers dictionary
2+
request_headers = JSON.parse(flow.getVariable("original.headers"))
3+
4+
# Loop through request headers and set them as response headers
5+
for key, value in request_headers.items():
6+
flow.setVariable("response.header.{0}".format(key), value)
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Access request headers dictionary
2-
request_headers = request.headers
2+
request_header_names = flow.getVariable("request.headers.names")
3+
request_headers = {}
4+
for name in request_header_names:
5+
request_headers[name] = flow.getVariable("request.header." + name)
6+
7+
# Store copy of original request headers
8+
flow.setVariable("original.headers", str(request_headers))
39

410
# Map of lowercase header name to desired parcel case header name
511
request_header_translation = {"x-request-id": "X-Request-ID", "x-correlation-id": "X-Correlation-ID"}
@@ -9,4 +15,9 @@
915
key = key.lower()
1016
desired_name = request_header_translation.get(key)
1117
if desired_name:
12-
flow.setVariable(f"request.header.{desired_name}", value)
18+
# Remove header with incorrect casing
19+
flow.removeVariable("request.header." + key)
20+
# Remove original header to avoid dupicated headers
21+
flow.removeVariable("request.header." + desired_name)
22+
# Set header with correct casing
23+
flow.setVariable("request.header." + desired_name, value)

proxies/live/apiproxy/targets/target.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,22 @@
6767
</Request>
6868
</Flow>
6969
</Flows>
70-
7170
<PostFlow>
7271
<Response>
7372
<Step>
7473
<Name>SetMimeType</Name>
7574
</Step>
75+
<Step>
76+
<Name>MirrorRequestHeaders</Name>
77+
</Step>
7678
</Response>
7779
</PostFlow>
7880
<FaultRules>
81+
<FaultRule name="default">
82+
<Step>
83+
<Name>MirrorRequestHeaders</Name>
84+
</Step>
85+
</FaultRule>
7986
<FaultRule name="401_Unauthorized">
8087
<Step>
8188
<Name>RaiseFault.401Unauthorized</Name>

0 commit comments

Comments
 (0)