I need to retrieve image content from GitHub using SourceGraph GraphQL API, base64 encode it and add it to an html and render it. When I base64 encode the image string and add it to html, image is not getting rendered. I used UTF-8 encoding. I tried with utf-16 or ascii encoding also but that didnt help.
When i download the same image , I am able to base64 encode the image and render it just fine.
Below is the code used-
GraphQL Query:
query {
repository(name: "RepoName") {
defaultBranch {
target {
commit {
blob(path: "Image file path") {
content
}
}
}
}
}
}
Snippet of the content returned ->"�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0004u\u0000\u0000\u0002�\b\u0006\u0000\u0000\u0000B��\u000e\u0000\u0000\u0000\u0001sRGB\u0000��\u001c�\u0000\u0000\u0000\u0004gAMA\u0000\u0000��\u000b�a\u0005\u0000
Python code -
response = requests.post(
url=self.api,
headers=self.Headers,
verify=CertName,
json={
"query": body,
"variables": {
"query": variables_query
}
}
)
if response.status_code == 200:
result = response.json()
resultSet = result["data"]["search"]["results"]["results"]
file_content_string = resultSet[0]["file"]["content"]
file_content_bytes = file_content_string.encode("utf-8")
file_content_string = base64.b64encode(file_content_bytes)