Decoding JSON that contains Base64
我正在向我的一个 API 发送对一组图像的请求。 API 以 JSON 格式返回这些图像。此格式包含有关资源的数据以及表示 Base64 图像的单个属性。
返回的 JSON 示例。
1 2 3 4 5 6 7 8 9 10 | { "id": 548613, "filename":"00548613.png", "pictureTaken":"2020-03-30T11:38:21.003", "isVisible": true, "lotcode": 23, "company":"05", "concern":"46", "base64":"..." } |
Base64的正确内容
错误解析的 Base64
这是通过 Python3 requests 库完成的。当我收到来自 API 的成功响应时,我尝试使用以下方法将正文解码为 JSON:
1 2 3 4 5 6 7 8 9 10 | url = self.__url__(f"/rest/all/V1/products/{sku}/images") headers = self.__headers__() r = requests.get(url=url, headers=headers) if r.status_code == 200: return r.json() elif r.status_code == 404: return None else: raise IOError( f"Error retrieving product '{sku}', got {r.status_code}: '{r.text}'") |
调用
我知道来自服务器的响应是正确的,它适用于 Postman,并且调用
的 JSON 文档
我将如何反序列化来自 API 的响应以获得有效的 Base64?
1 2 3 4 5 | import base64 import re ... b64text = re.search(b""base64": "(?P<base>.*)"", r.content, flags=re.MULTILINE).group("base") decode = base64.b64decode(b64text).decode(utf-8) |
既然你说"调用