Hugo "GetJSON" now support HTTP Header
TIL that hugo actually support custom HTTP headers to be use together with getJSON function which means you not anymore limited to just use or call API that have token=xxx on URL parameter anymore. Now you can use some header such as Authorization (example: Bearer, OAuth and more).
The implementation is quite simple just create a dictionary data and put the information inside and bind it together with getJSON as second parameter.
Example
Below is an example for my custom Hugo shortcode to get Malaysia forecast from MET API which require to send custom bearer Authorization on HTTP header for each request
{{/* Hugo shortcode to get Malaysia forecast from MET API | Copyright (c) 2021 Robbi Nespu | The MIT License (MIT) */}}
{{ $accessToken := "15b024277cc29a319df973c64289923220099f6e"}}
{{ $headers := dict "Authorization" (printf "METToken %s" $accessToken) }}
{{ with getJSON "https://api.met.gov.my/v2.1/data?datasetid=WARNING&datacategoryid=WINDSEA2&start_date=2021-10-09&end_date=2021-12-31" $headers }}
<table>
<tr><th>Date</th><th>From</th><th>To</th><th>Details</th></tr>
{{ range .results }}
<tr>
<td>{{ .attributes.valid_from | time.Format "02/01/06" }}</td>
<td>{{ .attributes.valid_from | time.Format ":time_short" }}</td>
<td>{{ .attributes.valid_to | time.Format ":time_full" }}</td>
<td>{{ .value.heading.en }}</td>
</tr>
{{ end }}
</table>
{{ end }}Please take note, this feature only available for released hugo version 0.84.0 and above. Thanks for the fish!
Have some thoughts, discussion or feedback on this post?
โธ What is webmention? How to send interactions!
webmention with this page.
You can also mention this URL on any website that supports WebMention.
Have you written a response to this post? Let me know the URL:Don't have WebMention? Use Comment Parade!
Comments section coming soon. For now, send a webmention or email.