Skip to content

Commit bae23e2

Browse files
NPA-8242: Working mirror request headers for unhappy path
1 parent e9ea4d1 commit bae23e2

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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)
1+
translated_header_names = ["X-Request-ID", "X-Correlation-ID"]
2+
for translated_header_name in translated_header_names:
3+
# Access original request headers dictionary
4+
original_request_header = flow.getVariable("original_request_header." + translated_header_name).split("=")
5+
# Remove original response header
6+
flow.removeVariable("response.header." + translated_header_name)
7+
# Mirror original request header
8+
flow.setVariable("response.header." + original_request_header[0], original_request_header[1])

proxies/live/apiproxy/resources/py/translate-request-headers.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ def get_request_headers():
1010
# Access request headers dictionary
1111
request_headers = get_request_headers()
1212

13-
# Store copy of original request headers
14-
flow.setVariable("original.headers", str(request_headers))
15-
1613
# Map of lowercase header name to desired parcel case header name
1714
request_header_translation = {"x-request-id": "X-Request-ID", "x-correlation-id": "X-Correlation-ID"}
1815

1916
# Loop through request headers
20-
for key, value in request_headers.items():
21-
desired_name = request_header_translation.get(key.lower())
22-
if desired_name:
23-
# Remove original header
24-
flow.removeVariable("request.header." + key)
17+
for original_header_name, header_value in request_headers.items():
18+
desired_header_name = request_header_translation.get(original_header_name.lower())
19+
if desired_header_name:
20+
# Store original header against the desired name for mirroring in response
21+
flow.setVariable(
22+
"original_request_header." + desired_header_name, "%s=%s" % (original_header_name, header_value)
23+
)
24+
# Remove original request header
25+
flow.removeVariable("request.header." + original_header_name)
2526
# Set header with correct casing
26-
flow.setVariable("request.header." + desired_name, value)
27+
flow.setVariable("request.header." + desired_header_name, header_value)

proxies/live/apiproxy/targets/target.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</Step>
4949
</Request>
5050
</Flow>
51-
<Flow name ="User Restricted">
51+
<Flow name="User Restricted">
5252
<Condition>accesstoken.auth_type = "user"</Condition>
5353
<Request>
5454
<Step>
@@ -111,4 +111,4 @@
111111
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property>
112112
</Properties>
113113
</HTTPTargetConnection>
114-
</TargetEndpoint>
114+
</TargetEndpoint>

0 commit comments

Comments
 (0)