API Gateway body template mapping to retain valid json data-structure of data-element, while still allow for base64-encoding

1

I have the exact same issue as an older question and there was even an accepted solution, but it has been deleted! The thread is: https://repost.aws/questions/QUm3sz-dEESBiezvcnxrnwRQ/api-gateway-body-template-mapping-to-retain-valid-json-data-structure-of-data-element-while-still-allow-for-base-64-encoding

The deleted answer is: https://forums.aws.amazon.com/thread.jspa?threadID=233060

Could someone please help me with this? This is a very frustrating issue.

Thanks.

1 Antwort
0

This is kinda hacky, but you need to work in strings a little further as once you are in json objects there doesn't seem a way to re-stringiify... Here is my snippet...

       { "StreamName": "event-stream-qa", "Records": [
#set($datakey = ".data")
#set($partitionkey = ".key")
#foreach($elem in $input.path('$.records'))
    #set($pathBegin = "$.records[")
    #set($pathEnd = "]")
    #set($currentIndex = $foreach.index)
    #set($dataPath = "$pathBegin$currentIndex$pathEnd$datakey")
    #set($partitionPath = "$pathBegin$currentIndex$pathEnd$partitionkey")
    #set($dataJson = $input.json($dataPath)) 
    #set($partitionJson = $input.json($partitionPath)) 
  { 
    "Data": "$util.base64Encode($dataJson)",
    "PartitionKey": $partitionJson
  }
    #if($foreach.hasNext),#end
#end  
  ]
}

ecordo
beantwortet vor einem Jahr

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen