Reputation: 33
I got the below response from a POST request and want to store the TOKEN value in an variable and use it for future use.
{"AccountName":"tester-01","token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCFtZSI6InN2Yy1kb2NwLXRlc3Rlcjkj5eWSZOSAExHVDL6V9_qgVOJ3pA","refreshvalue":"5X6g8QZ3rk1RWQXLdItXlKnfwTPGjQ==","validFrom":"2021-09-16T05:45:44Z","validTo":"2021-09-16T06:45:44Z"," refreshValidTo":"2021-09-16T11:45:44Z"}
The code snippet for the above response is
${body}= create dictionary username=test12345 password=12345
${header}= create dictionary Content-Type=application/json
${login}= post on session docp /login json=${body} headers=${header}
${value}= Set Variable ${login['token']}
When I try to set the response in the value variable I get the below error.
Resolving variable '${login['token']}' failed: TypeError: 'Response' object is not subscriptable.
Can someone please help.
Upvotes: 2
Views: 2330
Reputation: 20067
When you issue a request the returned data - the login
variable in your case - is a response object, not just the payload. It contains as properties the headers, payload, the request that was sent, and a lot others; see the library documentation, where it is described in details.
You're targeting the payload/the content of the respone; as you're dealing with a json api and expect it to be such, there is a convenience method that will return you that, parsed as a python dictionary:
${login}= post on session docp /login json=${body} headers=${header}
${payload}= Set Variable ${login.json()} # this will be the data you got from the service, as a plain dictionary
${value}= Set Variable ${payload['token']}
Upvotes: 3