Github JSON API Blob Binary data as Base64 encoded strings

跟風遠走 提交于 2019-12-24 11:35:07

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21804065/github-json-api-blob-binary-data-as-base64-encoded-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!