HUGO

getJson and HTTP headers

I understand using HTTP headers in templates is relatively new (since Hugo v0.84.0. I’m running v0.84.2.

I am trying to display elements from an API call. The cURL version of the API call is

curl --request GET \
     --url 'https://api.short.io/api/links?domain_id=178003&limit=150&before=lnk_abc_abc123' \
     --header 'Accept: application/json' \
     --header 'Authorization: xxxxxxxxxxxxxxxxxxxxx'

However I think my code does not seem to receive a JSON file and I am unable to call specific elements of the JSON file.

{{ $data := getJSON "https://api.short.io/api/links?domain_id=178003&limit=150&before=lnk_abc_abc123" (dict "Authorization" "xxxxxxxxxxxxxxxxxxxxx" "Accept" "application/json") }}
{{ range $data }}
    {{ . }}<hr>
{{end}}

Your syntax looks correct. We, by default, add ‘Accept: application/json’ so you could drop that, but I don’t see how that would have anything to do.

Isn’t your API expecting a hashed string of the Authorization token?

Sorted out the authentication but what I am getting back isn’t what I expect, or less able to show values for in the json array.
Instead of a json output like this

{
    "links": [
        {
            "id": 656298951,
            "path": "",
            "title": "Main page redirect (generated automatically)",
            "icon": null,
            "archived": false,
            "originalURL": "https://bromyardvets.co.uk/",
            "iphoneURL": null,
            "androidURL": null,
            "splitURL": null,
            "splitPercent": null,
            "expiresAt": null,
            "expiredURL": null,
            "redirectType": null,
            "clicksLimit": null,
            "cloaking": null,
            "source": "api",
            "integrationGA": null,
            "integrationFB": null,
            "integrationAdroll": null,
            "integrationGTM": null,
            "createdAt": "2021-06-21T11:00:57.000Z",
            "updatedAt": "2021-06-21T11:00:57.000Z",
            "DomainId": 178003,
            "Owner": null,
            "tags": [],
            "secureShortURL": "https://drugs.bromyardvets.co.uk/",
            "idString": "lnk_Kj1_IpLkX",
            "shortURL": "https://drugs.bromyardvets.co.uk/",
            "User": null
        },
        {
            "id": 643414529,
            "path": "Infla2.5",
            "title": "Inflacam 2.5 mg Chewable Tablets for Dogs",
            "icon": "https://shortio-icons.s3.amazonaws.com/e66c441f-7f99-4ecc-8e09-51f338ee6180",
            "archived": false,
            "originalURL": "https://www.vmd.defra.gov.uk/ProductInformationDatabase/product/A008382",
            "iphoneURL": null,
            "androidURL": null,
            "splitURL": null,
            "splitPercent": null,
            "expiresAt": null,
            "expiredURL": null,
            "redirectType": null,
            "clicksLimit": null,
            "cloaking": null,
            "source": "website",
            "integrationGA": null,
            "integrationFB": null,
            "integrationAdroll": null,
            "integrationGTM": null,
            "createdAt": "2021-06-10T14:05:38.000Z",
            "updatedAt": "2021-06-10T14:08:24.000Z",
            "DomainId": 178003,
            "Owner": {
                "id": 163589,
                "name": "Martin McDowell",
                "email": "martin.mcdowell@bromyardvets.co.uk",
                "photoURL": "https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9"
            },
            "tags": [],
            "secureShortURL": "https://drugs.bromyardvets.co.uk/Infla2.5",
            "idString": "lnk_Kj1_HxHvH",
            "shortURL": "https://drugs.bromyardvets.co.uk/Infla2.5",
            "User": {
                "id": 163589,
                "name": "Martin McDowell",
                "email": "martin.mcdowell@bromyardvets.co.uk",
                "photoURL": "https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9"
            }
        },
        {
            "id": 643413821,
            "path": "Infla1",
            "title": "Inflacam 1 mg Chewable Tablets for Dogs",
            "icon": "https://shortio-icons.s3.amazonaws.com/e759e924-5b8d-43f1-ad5f-ae6089562173",
            "archived": false,
            "originalURL": "https://www.vmd.defra.gov.uk/ProductInformationDatabase/product/A008381",
            "iphoneURL": null,
            "androidURL": null,
            "splitURL": null,
            "splitPercent": null,
            "expiresAt": null,
            "expiredURL": null,
            "redirectType": null,
            "clicksLimit": null,
            "cloaking": null,
            "source": "website",
            "integrationGA": null,
            "integrationFB": null,
            "integrationAdroll": null,
            "integrationGTM": null,
            "createdAt": "2021-06-10T14:04:58.000Z",
            "updatedAt": "2021-06-10T14:08:03.000Z",
            "DomainId": 178003,
            "Owner": {
                "id": 163589,
                "name": "Martin McDowell",
                "email": "martin.mcdowell@bromyardvets.co.uk",
                "photoURL": "https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9"
            },
            "tags": [],
            "secureShortURL": "https://drugs.bromyardvets.co.uk/Infla1",
            "idString": "lnk_Kj1_HxHkh",
            "shortURL": "https://drugs.bromyardvets.co.uk/Infla1",
            "User": {
                "id": 163589,
                "name": "Martin McDowell",
                "email": "martin.mcdowell@bromyardvets.co.uk",
                "photoURL": "https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9"
            }
        }
    ],
    "count": 271
}

I am getting this:

<p>271
3</p>
<p>[map[DomainId:178003 Owner:&lt;nil&gt; User:&lt;nil&gt; androidURL:&lt;nil&gt; archived:false clicksLimit:&lt;nil&gt; cloaking:&lt;nil&gt; createdAt:2021-06-21T11:00:57.000Z expiredURL:&lt;nil&gt; expiresAt:&lt;nil&gt; icon:&lt;nil&gt; id:6.56298951e+08 idString:lnk_Kj1_IpLkX integrationAdroll:&lt;nil&gt; integrationFB:&lt;nil&gt; integrationGA:&lt;nil&gt; integrationGTM:&lt;nil&gt; iphoneURL:&lt;nil&gt; originalURL:https://bromyardvets.co.uk/ path: redirectType:&lt;nil&gt; secureShortURL:https://drugs.bromyardvets.co.uk/ shortURL:https://drugs.bromyardvets.co.uk/ source:api splitPercent:&lt;nil&gt; splitURL:&lt;nil&gt; tags:[] title:Main page redirect (generated automatically) updatedAt:2021-06-21T11:00:57.000Z] map[DomainId:178003 Owner:map[email:martin.mcdowell@bromyardvets.co.uk id:163589 name:Martin McDowell photoURL:https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9] User:map[email:martin.mcdowell@bromyardvets.co.uk id:163589 name:Martin McDowell photoURL:https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9] androidURL:&lt;nil&gt; archived:false clicksLimit:&lt;nil&gt; cloaking:&lt;nil&gt; createdAt:2021-06-10T14:05:38.000Z expiredURL:&lt;nil&gt; expiresAt:&lt;nil&gt; icon:https://shortio-icons.s3.amazonaws.com/e66c441f-7f99-4ecc-8e09-51f338ee6180 id:6.43414529e+08 idString:lnk_Kj1_HxHvH integrationAdroll:&lt;nil&gt; integrationFB:&lt;nil&gt; integrationGA:&lt;nil&gt; integrationGTM:&lt;nil&gt; iphoneURL:&lt;nil&gt; originalURL:https://www.vmd.defra.gov.uk/ProductInformationDatabase/product/A008382 path:Infla2.5 redirectType:&lt;nil&gt; secureShortURL:https://drugs.bromyardvets.co.uk/Infla2.5 shortURL:https://drugs.bromyardvets.co.uk/Infla2.5 source:website splitPercent:&lt;nil&gt; splitURL:&lt;nil&gt; tags:[] title:Inflacam 2.5 mg Chewable Tablets for Dogs updatedAt:2021-06-10T14:08:24.000Z] map[DomainId:178003 Owner:map[email:martin.mcdowell@bromyardvets.co.uk id:163589 name:Martin McDowell photoURL:https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9] User:map[email:martin.mcdowell@bromyardvets.co.uk id:163589 name:Martin McDowell photoURL:https://www.gravatar.com/avatar/78dc65ceb631fbadfc588d962ed6dac9] androidURL:&lt;nil&gt; archived:false clicksLimit:&lt;nil&gt; cloaking:&lt;nil&gt; createdAt:2021-06-10T14:04:58.000Z expiredURL:&lt;nil&gt; expiresAt:&lt;nil&gt; icon:https://shortio-icons.s3.amazonaws.com/e759e924-5b8d-43f1-ad5f-ae6089562173 id:6.43413821e+08 idString:lnk_Kj1_HxHkh integrationAdroll:&lt;nil&gt; integrationFB:&lt;nil&gt; integrationGA:&lt;nil&gt; integrationGTM:&lt;nil&gt; iphoneURL:&lt;nil&gt; originalURL:https://www.vmd.defra.gov.uk/ProductInformationDatabase/product/A008381 path:Infla1 redirectType:&lt;nil&gt; secureShortURL:https://drugs.bromyardvets.co.uk/Infla1 shortURL:https://drugs.bromyardvets.co.uk/Infla1 source:website splitPercent:&lt;nil&gt; splitURL:&lt;nil&gt; tags:[] title:Inflacam 1 mg Chewable Tablets for Dogs updatedAt:2021-06-10T14:08:03.000Z]]
3</p>

Do I need to specify encoding when executing the getJson function?

Either there is some missing operand or there is a bug in the code. Tried using different API requests

https://postman-echo.com/get?foo1=bar1&foo2=bar2

Also tried disabling cache with

hugo server -D --ignoreCache

What, specifically, isn’t working?

This:

{{ $data := getJSON "https://postman-echo.com/get?foo1=bar1&foo2=bar2" (dict "Authorization" "foo") }}
<pre>{{ jsonify (dict "indent" "  ") $data }}</pre>

Produces:

{
  "args": {
    "foo1": "bar1",
    "foo2": "bar2"
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip",
    "authorization": "foo",
    "host": "postman-echo.com",
    "user-agent": "Hugo Static Site Generator",
    "x-amzn-trace-id": "Root=1-60f59cb9-0c9cacbd467f8be05c5013c8",
    "x-forwarded-port": "443",
    "x-forwarded-proto": "https"
  },
  "url": "https://postman-echo.com/get?foo1=bar1\u0026foo2=bar2"
}
1 Like

I think the misunderstanding is that getJSON returns a map (JSON decoded into map). Yes, I see the irony. If you really want it as a JSON string, see @jmooring 's example.