Error about Resolving variable '$uri["aaa"] failed: TypeError: string indices must be integers

i have a problem with use variable in yaml file

my variable is type string but execute is have type error:string indices must be integers.

this is my yaml file
uri:
aaa: “/api/aa”

my code robot
*** Test Cases ***
TC001
Request Withdraw

*** Keywords ***
1.
Request Withdraw
${response}= POST On Session ${SESSION_NAME} ${uri[“aaa”]} headers=${header} json=${request_withdraw} expected_status=200

execute is fail

but I use
2.
${response}= POST On Session ${SESSION_NAME} /api/aa headers=${header} json=${request_withdraw} expected_status=200

I can execute is pass

I don’t understand why i can’t write no.1

I can solve the problem. because I declare a duplicate variable

TypeError: means that you are trying to perform an operation on a value whose type is not compatible with that operation. An Iterable is a collection of elements that can be accessed sequentially . In Python, iterable objects are indexed using numbers . When you try to access an iterable object using a string or a float as the index, an error will be returned as TypeError: string indices must be integers. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value.

For example, str[hello"] and str[2.1] as indexes. As these are not integers, a TypeError exception is raised. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value . If you are accessing items from a dictionary , make sure that you are accessing the dictionary itself and not a key in the dictionary.

Python supports slice notation for any sequential data type like lists, strings , tuples, bytes, bytearrays, and ranges. When working with strings and slice notation, it can happen that a TypeError: string indices must be integers is raised, pointing out that the indices must be integers, even if they obviously are.

Hi Nareenart,

As you didn’t show the actual error you got and the information you did giv looks to be missing important detail, so it’s impossible for anyone to know what the problem is.

So lets go back to first principles:

  • I don’t see where you included the yaml file (typically in the *** Settings *** section of the robot file)
    • Formatting is important in a yaml file, does your yaml look like Example A below?
    • or Example B?
    • or something else?
  • When you get an unexpected error it’s often helpful to know what the contents of the variable is, as this will lead you to the issue more quickly. The BuiltIn Log is really useful for this. In your Request Withdraw test case I suggest you add the following lines before POST On Session
    • Log ${uri}
    • Log ${uri["aaa"]}
    • Log ${aaa}
    • Log ${uri}["aaa"]
  • Also note that and " are different characters, not sure if this was just something that was auto changed by you computer but your post uses in the variable ${uri[“aaa”]} when it should use " like this ${uri["aaa"]}, the same applies to the yaml file

Example A

uri:
aaa: "/api/aa"

Example B

uri:
    aaa: "/api/aa"

Hopefully something here will help,

Dave.