Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding authorization header in robot-framework

I am learning robot-framework, API automation:

*** Settings ***
Library  RequestsLibrary
Library  Collections
Library  String

*** Variables ***
${headers}       Create Dictionary  Authorization Bearer abcde


*** Test Cases ***
Make a simple REST API call
    [Tags]  API
    Create Session  my_json  http://localhost:3000
    Log  ${headers}
    ${response} =  Get Request  my_json  /posts   headers=${headers}
    Log  ${response}
    # Check the Response status
    Should Be Equal As Strings  ${response.status_code}  403
#    ${response} =  Get Request  my_json /posts

    ${json} =  Set Variable  ${response.json()}
    Log  ${json}
    Log  len(${json})
    Should Be Equal As Strings  ${json['name']}  rajesh

I am getting this error in log.html

Documentation: Send a GET request on the session object found using the given alias

Start / End / Elapsed: 20181209 18:43:04.159 / 20181209 18:43:04.175 / 00:00:00.016 18:43:04.175 FAIL AttributeError: 'str' object has no attribute 'items'

like image 749
raju Avatar asked Mar 04 '26 03:03

raju


1 Answers

I think only create dictionary object needs to be changed. You should pass key and value to it. Refer link BuildIn(create Dictionary)

*** Settings ***
    Library  RequestsLibrary
    Library  Collections
    Library  String

    *** Variables ***
    ${headers}       Create Dictionary  Authorization=“Bearer abcde”


    *** Test Cases ***
    Make a simple REST API call
        [Tags]  API
        Create Session  my_json  http://localhost:3000
        Log  ${headers}
        ${response} =  Get Request  my_json  /posts   headers=${headers}
        Log  ${response}
        # Check the Response status
        Should Be Equal As Strings  ${response.status_code}  403
    #    ${response} =  Get Request  my_json /posts

        ${json} =  Set Variable  ${response.json()}
        Log  ${json}
        Log  len(${json})
        Should Be Equal As Strings  ${json['name']}  rajesh
like image 180
Yash Jagdale Avatar answered Mar 06 '26 17:03

Yash Jagdale