简体   繁体   中英

How to keep existing event fields when creating an AWS API Gateway Template

Per an answer to this question I tried a mapping template as follows. I had a variable called "body" being passed which was a JSON structure with several child fields, and now that is gone.

{
   "client_ip" : "$input.params('X-Forwarded-For')",
   "user_agent" : "$input.params('User-Agent')"
}

I tried adding it like this, but still not getting the "body" with its child fields.

{
   "client_ip" : "$input.params('X-Forwarded-For')",
   "user_agent" : "$input.params('User-Agent')",
   "body": "$input.params('body')"
}

Here is a sample of a my "body" element and some of the child fields:

2021-06-06T13:30:01.231-05:00   pprint event:
2021-06-06T13:30:01.231-05:00   {'body': {'ResponseTimeMilliseconds': 2225,
2021-06-06T13:30:01.231-05:00   'authToken': '12312312',
2021-06-06T13:30:01.231-05:00   'handNumber': 7}}

Should I use

{
   "client_ip" : "$input.params('X-Forwarded-For')",
   "user_agent" : "$input.params('User-Agent')",
   "body" : $input.json('body') 
}

or

{
   "client_ip" : "$input.params('X-Forwarded-For')",
   "user_agent" : "$input.params('User-Agent')",
   "body" : $input.json('$') 
}

or hopefully I do not have to spell out all the child fields.

I just have one test environment, and every time I try something that doesn't work, I'm impacting other people.

I've been referencing this page: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

The following template worked for me:

{
   "client_ip" : "$input.params('X-Forwarded-For')",
   "user_agent" : "$input.params('User-Agent')",
   "body" : $input.json('$.body') 
}

NOTE: if I put $input.json('$') or $input.json('body'), I ended up with a "body" field inside my "body" field, which broke the current logic.

Results in log (notice the close "}" at the end of the 'body" section and before 'client_ip'. I didn't really need user_agent, so I could remove that.

2021-06-07T18:34:21.371-05:00   {'body': {'ResponseTimeMilliseconds': 333831,
2021-06-07T18:34:21.371-05:00   'authToken': '12312312',
2021-06-07T18:34:21.371-05:00   'handNumber': 7},
2021-06-07T18:34:21.371-05:00   'client_ip': '70.122.142.41',
2021-06-07T18:34:21.371-05:00   'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
2021-06-07T18:34:21.371-05:00   '(KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM