Search code examples
aws-appsync

encoding and decoding base64.in aws appsync resolver


I have a resolver that receives an argument "nextToken" ($ctx.args.nextToken) which will be in base64 string. In the request mapping template, I need to convert nextToken from base64 string into ascii string. I know that Appsync has $util.base64Decode(String) : byte[] but this function gives back byte[] but I want back an ascii string.

In addition, I will need to create base64 string from ascii string in the response mapping template. Again Appsync provides a function $util.base64Encode( byte[] ) : String but I don't know how to change my ascii string into byte[].

Anybody has any idea how to handle both situations. Thanks in advance.


Solution

  • VTL template $util functions for base64 accept a string and return a string.

    Example VTL template:

    #set($base64 = $util.base64Encode("Hello World"))
    {
      "label": "$util.base64Decode($base64)"
    }
    

    Returns "Hello World"