abdessamad
abdessamad

Reputation: 25

how to use Session Token in rest API

im tried to use this code and does not work i have to get Session token by using the first keyword and use the session token in second keyword but error message displayed : AttributeError: 'str' object has no attribute 'items'

Get SessionToken
            Create Session    session    https://url    verify=True
            ${resp}=    Get Request    session    uri?appToken=xxxxxx&userId=xxxx&password=xxxx
            Should Be Equal As Strings    ${resp.status_code}    200
            ${json} =  Set Variable  ${resp.json()}
            Dictionary Should Contain Key    ${json}[0]    sessionToken
            ${SessionToken}=    Get From Dictionary    ${json["result"]}    sessionToken
            [Return]    ${SessionToken}

        Check Transaction Detail MCM
            ${headers}=   Set Variable     Get SessionToken
            Create Session    session  https://url   verify=True
            ${resp}=   Get Request    session    /token   URI    headers = ${headers}

Upvotes: 2

Views: 398

Answers (1)

Todor Minakov
Todor Minakov

Reputation: 20077

The Set Variable keyword is used to assign a static (e.g. "predefined") value to a variable. In a programming language this line

${headers}=   Set Variable     Get SessionToken

would be equivalent to:

headers =  "Get SessionToken"

The variable ${headers} now has as a value the string "Get SessionToken", regardless there is a keyword with the same name (for the framework that's just a coincidence (in fact, the framework doesn't "care" or "know" for this coincidence)).

If you want to store the return value of the keyword Get SessionToken - then just assign it:

${headers}=     Get SessionToken

${headers} will now have as a value whatever the content of ${SessionToken} was.

You could be even more explicit with this construct:

${headers}=     Run Keyword    Get SessionToken

, but that's just bad style (Run Keyword has very solid usages, just not in simple/trivial constructs like this).


Bear in mind the headers parameter in the Get Request keyword expects a dictionary, so be sure what ends up there is of this type.
This was precisely the error you got - as you've assigned a string value to the argument, it failed with the exception it's not a dict. (technically, it raised the exception a string object doesn't have the items() method - e.g. the keyword presumes the type and tries to use it as a dict)

Upvotes: 4

Related Questions