Search code examples
pythonpython-3.xrobotframework

Robotframework - AttributeError: 'str' object has no attribute 'items'


I am trying to make a simple POST to the API but keep getting Attribute Error: 'str' object has no attribute 'items'. I have tried using a different API to make a POST request and it works without any problems. The difference was that it didn't require header information.

*** Settings ***
Library     RequestsLibrary
Library     Collections
Library     OperatingSystem

*** Variables ***
${baseUrl}      https://simple-books-api.glitch.me
${headers}      Content-Type=application/json;charset=utf-8   Authorization=Bearer 9daaef9d38b7bff40af6400bba8fffb7a466e778ecd3068d9a9766d231491bc2


*** Test Cases ***
OrderBook
create session  mysession   ${baseUrl}  verify=true
${body}=    create dictionary       bookId=1  customerName=John Doe
LOG TO CONSOLE     ${body}
${response}=    post on session    mysession   /orders     data=${body}    headers=${headers}
log to console  ${response}

log to console    ${response.status_code}
log to console    ${response.content}

#validations
${res_body}=    convert to string   ${response.content}
should contain    ${res_body}   true

Solution

  • The issue came from the way you defined the ${headers} variable - in the Variables section you need to prefix with & to get a dictionary. Just change it to this and it should be fine:

    *** Variables ***
    ${baseUrl}      https://simple-books-api.glitch.me
    &{headers}      Content-Type=application/json;charset=utf-8   Authorization=Bearer 9daaef9d38b7bff40af6400bba8fffb7a466e778ecd3068d9a9766d231491bc2