How do I just serve some simple JSON at a given URL?

Hi. I just want a URL that looks like: http://my-site.com/json-route/

The content should be of type application/json and should be as follows:

{
    "hello": "world"
}

Hi @jononomo - you will need to put the json file in your static directory under \json-route. The contents of the static directory get copied intact to the root directory of your generated site.

2 Likes

Thanks much, that worked. I had to take one extra step to get it to serve correctly after I deployed to Amazon S3, however – it was defaulting to to Content-Type="binary/octet" or something like that and I had to manually change the content type for the file in the AWS console to set it to “application/json”. Is there any way I can configure Hugo so that it defaults to content-type="application/json" when it is served? I would like to have it default to this content type regardless of whether the file ends with a .json extension.

Hi @jononomo - this isn’t a Hugo issue I’m afraid as Hugo isn’t responsible for setting the http headers - it just generates the content files for the web server. I had a quick google and it sounds like a common problem when uploading files to S3. There are a couple of discussions on stackoverflow that may point you in the right direction, but in summary, it sounds like you need to find an upload tool that sets the right defaults each time you upload. Here are a couple of SO threads for you:


and one straight from the AWS forums:
https://forums.aws.amazon.com/thread.jspa?messageID=224446

1 Like

Wow, thanks - very helpful.