I'm requesting some JSON data from a pod's web server via the Kubernetes API proxy verb. That is:
corev1 = kubernetes.client.CoreV1Api()
res = corev1.connect_get_namespaced_pod_proxy_with_path(
'mypod:5000', 'default', path='somepath', path2='somepath')
print(type(res))
print(res)
The call succeeds and returns a str
containing the serialized JSON data from my pod's web service. Unfortunately, res
now looks like this ... which isn't valid JSON at all, so json.loads(res)
denies to parse it:
{'x': [{'xx': 'xxx', ...
As you can see, the stringified response looks like a Python dictionary, instead of valid JSON. Any suggestions as to how this convert safely back into either correct JSON or a correct Python dict
?
Looking at the source code for core_v1_api.py. The method calls generally accept a kwarg named _preload_content
.
Setting this argument to False
instructs the method to return the urllib3.HTTPResponse
object instead of a processed str
. You can then work directly with the data, which cooperates with json.loads()
.
Example:
corev1 = client.CoreV1Api()
res = corev1.connect_get_namespaced_pod_proxy_with_path(
'mypod:5000', 'default', path='somepath',
path2='somepath', _preload_content=False)
json.loads(res.data)