Kubernetes Python client returns pod's JSON HTTP response from proxy verb as string with single quotes instead of double quotes

4/25/2019

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?

-- TheDiveO
json
kubernetes
kubernetes-python-client
proxy

1 Answer

4/25/2019

After reading through some code of the Kubernetes Python client, it is now clear that connect_get_namespaced_pod_proxy() and connect_get_namespaced_pod_proxy_with_path() force the response body from the remote API call to be converted to str by calling self.api_client.call_api(..., response_type='str', ...) (core_v1_api.py). So, we're stuck with the Kubernetes API client giving us only the string representation of the dict() representing the original JSON response body.

To convert the string back to a dict(), the anwer to Convert a String representation of a Dictionary to a dictionary? suggests to use ast.literal_eval(). Wondering whether this is a sensible route to take, I've found the answer to Is it a best practice to use python ast library for operations like converting string to dict says that it's a sensible thing to do.

import ast
corev1 = kubernetes.client.CoreV1Api()
res = corev1.connect_get_namespaced_pod_proxy_with_path(
    'mypod:5000', 'default', path='somepath', path2='somepath')
json_res = ast.literal_eval(res)
-- TheDiveO
Source: StackOverflow