Github JSON API Blob Binary data as Base64 encoded

2019-07-27 21:49发布

When I make this request to the Github API

curl -H "Accept: application/vnd.github.v3.raw" \
  https://api.github.com/repos/STRd6/SurfN-2-Sur5/git/blobs/cf0167baab25ea841834fee66167ca33af28a7bf

I receive:

鐎G

IHDR ٳ⿀bKGD̿   pHYsHHFɫ>   vpAg  纜퀀剄ATHǭきఈEaᙜ扊ᦒă彾
4Gᦇ߸�SDԱSЍ:詎⮢⍯5Ɏ搩Ta2ك2xқ?⳷ṵࡓ瑛躒ቬ쐼뀛է鑳ԷWꇎsો:F坠ꀣ%tEXtdate:create2011-08-20T14:56:12-07:00ራ뀀%tEXtdate:modify2011-08-20T14:56:12-07:00ՓIEND 

When I expect to receive something more like:

�PNG

IHDR szz�IDATXGU�Y���������c����.aQ$�(��<E�1��?

I believe this is caused by the Github API encoding the binary data incorrectly as UTF-8 instead of ISO-8859-1.

The problem even occurs when the data is Base64 encoded through the github API as well:

♥ curl -H "Content-Type: application/json"  https://api.github.com/repos/STRd6/SurfN-2-Sur5/git/blobs/cf0167baab25ea841834fee66167ca33af28a7bf
{
  "sha": "cf0167baab25ea841834fee66167ca33af28a7bf",
  "size": 358,
  "url": "https://api.github.com/repos/STRd6/SurfN-2-Sur5/git/blobs/cf0167baab25ea841834fee66167ca33af28a7bf",
  "content": "6ZCORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZs+K/gAAAAmJLR0QA74eP\nzL8AAAAJcEhZcwAAAEgAAABIAEbJqz4AAAAJdnBBZwAAACAAAAAgAOe6nO2A\ngADliYRBVEjHreOBjeCwiEXvhpkZYRzhmZwFD+aJiuGmksSD75KL5b2+Csmg\nCDRHBeGmh+6rhN+477+97qyQUxxE1LFT0I066KmO7pOw4q6i4o2vNcmO7rSq\n5pCpVAZhAzLZgzJ40ps/4rO34bm1EuChk+eRm+i6kuGJrOyQvAfrgJvVp+mR\nsx3Ut1fqh45z4KuLOgBG5Z2gEOqAowAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAx\nMS0wOC0yMFQxNDo1NjoxMi0wNzowMOGIq+uAgAAldEVYdGRhdGU6bW9kaWZ5\nADIwMTEtMDgtMjBUMTQ6NTY6MTItMDc6MDDVkwcAAAAASUVORO6CoOKAgA==\n",
  "encoding": "base64"
}

Notice that the PNG data begins with 6ZCORw0KGgoA... instead of the usual iVBORw0KGg... which shows up when PNGs are Base64 encoded.

What I want to know is there some way to specify that the data is binary and should be read out to me as such?

2条回答
Fickle 薄情
2楼-- · 2019-07-27 22:20

To answer your question (copied below):

What I want to know is there some way to specify that the data is binary and should be read out to me as such?

The answer is yes. The solution is in the documentation.

You need to send the following header:

Accept: application/vnd.github.VERSION.raw

Where VERSION is the version of the GitHub API you wish to interact with.

查看更多
别忘想泡老子
3楼-- · 2019-07-27 22:32

It looks like the encoding of your blobs was set incorrectly to UTF-8 when you uploaded them to github via the API.

To resolve this make sure that you choose the correct encoding when uploading your blobs and the Github API will return them correctly.

查看更多
登录 后发表回答