Submit JSON data from Github Pages

I have a github repository that contains several csv, json, yml data files in the root directory of the repository.

How do I get it to serve these files using Github Pages?

So, for example, when I go to http://username.imtqy.com/reponame/ , followed by

  • /posts - serve contents of posts.json file
  • /users - serve contents of users.json file
  • /comments - serve the contents of the comments.json file
  • /posts.csv - serve contents of posts.csv file
+5
source share
2 answers

You just need to add the .json files to your repository, and you can access them like a regular JSON API

In the following username.imtqy.com/reponame repository add the following files

posts.json

 [ {"id": 1, "title": "Title 1"}, {"id": 2, "title": "Title 2"} ] 

users.json

 [ {"name": "abc", "email": " abc@example.com "}, {"name": "xyz", "email": " xyz@example.com "} ] 

comments.json

 [ {"post_id": "1", "text": "Comment 1"}, {"post_id": "2", "text": "Comment 2"} ] 

posts.csv

 id,title 1,Title 1 2,Title 2 

And access them using

  • http://username.imtqy.com/reponame/posts.json
  • http://username.imtqy.com/reponame/users.json
  • http://username.imtqy.com/reponame/comments.json
  • http://username.imtqy.com/reponame/posts.csv
+4
source

This type of URL (for example: / posts) will only work with html files. You can name your json posts.html file and set its front end as follows:

 --- layout: null permalink: /posts/ --- { "variable": "value" } 

Then you will reach your file in / posts or / posts / .

The only drawback is that the returned file is /posts/index.html , which is served by the mime Content-Type: text/html , rather than the expected application/json .

0
source

All Articles