22

I came across a gist which returns JSON data https://gist.githubusercontent.com/rdsubhas/ed77e9547d989dabe061/raw/6d7775eaacd9beba826e0541ba391c0da3933878/gnc-js-api

I tried to create one to return JSON data and ended up like this https://gist.github.com/vigneshvdm/862ec5a97bbbe2021b79

How can i create a link like the first one and make it return data in JSON format

1
  • Well if you're looking for full JSON storage on your github gist, I would recommend you JSONGist.io Commented Feb 4, 2022 at 10:15

4 Answers 4

15

The problem is the HTTP response type is text/plain but you will need application/json for most clients to handle it properly.

Update: using rawgit.com I was able to get your test working with the correct content-type.

My test Gist: https://gist.githubusercontent.com/anonymous/85dbc2c71023f24c2e26/raw/849848a71a1805a314897f9fe98eb7dc43e2e9b9/gistfile1.json

My RawGit URL: https://rawgit.com/anonymous/85dbc2c71023f24c2e26/raw/849848a71a1805a314897f9fe98eb7dc43e2e9b9/gistfile1.json

Using HTTP GET, sending over:

GET https://rawgit.com/anonymous/85dbc2c71023f24c2e26/raw/849848a71a1805a314897f9fe98eb7dc43e2e9b9/gistfile1.json HTTP/1.1
Accept: application/json
Host: rawgit.com

Receiving back:

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 24 Dec 2014 10:57:07 GMT
Content-Type: application/json
Connection: keep-alive
X-Content-Type-Options: nosniff
X-Robots-Tag: none
RawGit-Naughtiness: 0
Access-Control-Allow-Origin: *
ETag: "0250189db62d31523a5cd0da47449eb4"
Cache-Control: max-age=300
Vary: Accept-Encoding
RawGit-Cache-Status: HIT
Content-Length: 104

[{ Name: "Vignesh", Salary: 30000 },{ Name: "Yuvraj", Salary: 90000 },{ Name: "Nithya", Salary: 87000 }]

And a couple screenshots:

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

How can i change the content type?
That's controlled on the server and it doesn't appear that GitHub is providing that ability. If you're responsible for the client that is handling it, you can most likely make it work by having it ignore the type - or throw together a light-weight proxy web server that downloads the string of text and reserves it with that HTTP header... What language/technology is beign used on the cilent?
can i add something like JSON.parse(data) to convert it to json?
I'm trying to do the same but for a .js file and it seems like it not returning any content-type header at all. Does anyone else experience this?
Are you using Fiddler to see this? Do you send the Accepts header first? Accepts tells the server what capabilities the client has (or what it's expecting), and based on that the server should respond with the right headers and info
|
7
  1. Put your json in a new gist.
  2. Look for the option raw. Click it and you have plain text version of whatever you had.

One thing to note is that it appears json data, but the headers reveal - Content-Type:text/plain

You can use https://rawgit.com/ for modifying the headers.

4 Comments

how should i change it to json?
You cant. When the github server renders the response, it sets the content type as text/plain. Unless you run your own server or reverse-proxy the request through another server which modifies the headers.
@vignesh you would want to save your file as a .json file while creating the gist
RawGit has reached the end of its useful life
3

I think the best option to do this is directly using Github REST API:

GET https://api.github.com/gists/:GIST_ID HTTP/1.1
Accept: application/json

Then, you can get the latest file content(JSON) from the response at path "files.{FILE_NAME}.content"

The response also contains several other useful things like history and author information, timestamps, etc.

This eliminates several disadvantages of other approaches:

  1. Using raw gist url is a static url for a particular revision of the gist. If the gist gets updated, the raw gist url will point to older revision
  2. rawgit is shutting down so using that will prove fatal to your project.
  3. Directly fetching from gist url. You'll have to parse html. No necessito! ¯\_(ツ)_/¯

Comments

0
  1. Paste the valid JSON text into the gist.
  2. Give it a file name that ends in .json .
  3. Create the gist.
  4. Now just select the raw button and use this url for doing your get request.

Works for me.

The headers say it is text but it's valid json.

Your gist is not valid JSON. I think that's the issue for you. Keys must be strings. https://jsonlint.com/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.