base64.md (1597B)
1 --- 2 title: base64 3 description: "`base64Encode` and `base64Decode` let you easily decode content with a base64 encoding and vice versa through pipes." 4 date: 2017-02-01 5 publishdate: 2017-02-01 6 lastmod: 2017-02-01 7 categories: [functions] 8 menu: 9 docs: 10 parent: "functions" 11 keywords: [] 12 relatedfuncs: [] 13 signature: ["base64Decode INPUT", "base64Encode INPUT"] 14 workson: [] 15 hugoversion: 16 deprecated: false 17 draft: false 18 aliases: [] 19 --- 20 21 An example: 22 23 {{< code file="base64-input.html" >}} 24 <p>Hello world = {{ "Hello world" | base64Encode }}</p> 25 <p>SGVsbG8gd29ybGQ = {{ "SGVsbG8gd29ybGQ=" | base64Decode }}</p> 26 {{< /code >}} 27 28 {{< output file="base-64-output.html" >}} 29 <p>Hello world = SGVsbG8gd29ybGQ=</p> 30 <p>SGVsbG8gd29ybGQ = Hello world</p> 31 {{< /output >}} 32 33 You can also pass other data types as arguments to the template function which tries to convert them. The following will convert *42* from an integer to a string because both `base64Encode` and `base64Decode` always return a string. 34 35 ``` 36 {{ 42 | base64Encode | base64Decode }} 37 => "42" rather than 42 38 ``` 39 40 ## `base64` with APIs 41 42 Using base64 to decode and encode becomes really powerful if we have to handle 43 responses from APIs. 44 45 ``` 46 {{ $resp := getJSON "https://api.github.com/repos/gohugoio/hugo/readme" }} 47 {{ $resp.content | base64Decode | markdownify }} 48 ``` 49 50 The response of the GitHub API contains the base64-encoded version of the [README.md](https://github.com/gohugoio/hugo/blob/master/README.md) in the Hugo repository. Now we can decode it and parse the Markdown. The final output will look similar to the rendered version on GitHub.