4

I want to access session_key from JSON response. In login api I am sending credential it response back with Content-Type: application/json I need to access session_key. How to access session_key for json response.

credential["username"]="john"
credential["password"]="xxx"
response =c.put('/api/login', data=json.dumps(credential))

JSON Respone

  print response.content

output

{"message": "", "result": {"username": "john", "session_key": "xyx"}}

When I tried to use print(response.content.result) getting error AttributeError: 'str' object has no attribute 'result'

2 Answers 2

7

If response.content is json you just have to decode it to a python dict.

import json

content = json.loads(response.content)
key = content['result']['session_key']
Sign up to request clarification or add additional context in comments.

Comments

2

In request.content you have json data.. you have to get that data to python dict..

You can try this:

json.loads(response.content)['result']['session_key']

or if you use requests:

response.content.json()['result']['session_key']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.