Python Forum
"TypeError: string indices must be integers, not 'str'" while not using any indices
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"TypeError: string indices must be integers, not 'str'" while not using any indices
#1
Hi guys! Currently trying to code my first program to automate an API, but got stuck at the moment where I transfer 2 arguments from earlier functions to the 3-rd function.

BTW, feel free to leave any suggestions for the whole code, apart for the error.

Here I'm authenticating and extract the cookie token:

"""HTTP-methods for booker API"""
import requests
import json

from workfiles import general_http_methods

base_url = 'https://restful-booker.herokuapp.com'

"""Authentication"""


def booker_auth_method():
    auth_resource = '/auth'
    auth_url = base_url + auth_resource
    print(auth_url)
    auth_body = {'username': 'admin',
                 'password': 'password123'}
    result_auth_token = general_http_methods.general_post_method(auth_url, auth_body)
    json_result_auth_token = result_auth_token.json()
    value_of_the_cookie_json_result_auth_token = str(json_result_auth_token.get('token'))
    adding_token_to_auth_token_value = 'token=' + value_of_the_cookie_json_result_auth_token
    result_auth_cookie_token = str ({'Cookie': adding_token_to_auth_token_value})
    print(result_auth_cookie_token)
    return result_auth_cookie_token


booker_auth_method_var = booker_auth_method()
Then I create a booking and return its id in the str form:

"""Creating a booking"""

def booker_create_booking_method():
    create_booking_resource = '/booking'
    create_booking_body = {
        "firstname": 'Python',
        "lastname": "Tester",
        "totalprice": 111,
        "depositpaid": True,
        "bookingdates": {
            "checkin": "2023-01-01",
            "checkout": "2023-01-02"},
        "additionalneeds": "Breakfast"}
    create_booking_url = base_url + create_booking_resource
    print(create_booking_url)
    create_booking_result = general_http_methods.general_post_method(create_booking_url, create_booking_body)
    str_json_create_booking_result = str(create_booking_result.json()['bookingid'])
    print(str_json_create_booking_result)
    return str_json_create_booking_result

booker_create_booking_method_var = booker_create_booking_method()
Finally, I wanna pass the return values from the 2 previous functions into the function below. To do that, I saved the return values in the variables (booker_auth_method_var, booker_create_booking_method_var) and pass them as arguments into my next function.

"Updating the WHOLE booking"

def booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var):
    update_booking_resource = '/booking/'
    update_booking_url = base_url + update_booking_resource + booker_create_booking_method_var
    update_booking_body = {
        "firstname": "Python",
        "lastname": "QA Engineer",
        "totalprice": 777,
        "depositpaid": False,
        "bookingdates": {
            "checkin": "2023-07-08",
            "checkout": "2023-07-15"},
        "additionalneeds": "ALL INCLUSIVE"}
    update_booking_result = general_http_methods.general_put_method(update_booking_url, booker_auth_method_var, update_booking_body)
    print(booker_auth_method_var, update_booking_url, update_booking_result.text)
    return update_booking_result


booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var)
And that's where code goes crazy and I get the following output:

/Users/.../python /Users/evgeniy/PycharmProjects/booker_automation/workfiles/booker_http_methods.py
https://restful-booker.herokuapp.com/auth
{'Cookie': 'token=366401ff5eabf91'}
https://restful-booker.herokuapp.com/booking
1820 <Response [200]>
Error:
Traceback (most recent call last): File "/Users/.../booker_http_methods.py", line 73, in <module> booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var) File "/Users/.../booker_http_methods.py", line 68, in booker_update_booking_method update_booking_result = general_http_methods.general_put_method(update_booking_url, booker_auth_method_var, update_booking_body) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../general_http_methods.py", line 20, in general_put_method result = requests.put(url, headers=headers, cookies=cookies, json=body) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../api.py", line 130, in put return request("put", url, data=data, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../sessions.py", line 573, in request prep = self.prepare_request(req) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../sessions.py", line 471, in prepare_request cookies = cookiejar_from_dict(cookies) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/.../cookies.py", line 537, in cookiejar_from_dict cookiejar.set_cookie(create_cookie(name, cookie_dict[name])) ~~~~~~~~~~~^^^^^^ TypeError: string indices must be integers, not 'str'
Process finished with exit code 1

My another python file "general_http_methods" which I import to the file with the error contains the following:

"""General HTTP-methods"""

import requests

headers = {
    'Content-Type':'application/json',
    'Accept':'application/json'}

def general_get_meothod(url):
    result = requests.get(url)
    return result


def general_post_method(url, body):
    result = requests.post(url, headers=headers, json=body)
    return result


def general_put_method(url, cookies, body):
    result = requests.put(url, headers=headers, cookies=cookies, json=body)
    return result


def general_patch_method(url, cookies, body):
    result = requests.patch(url, headers=headers, cookies=cookies, json=body)
    return result


def general_delete_method(url, cookies, body):
    result = requests.patch(url, headers=headers, cookies=cookies, json=body)
    return result
Please, help. This mistake kind freaks me out cos I don't understand neither its reasons nor the way to fix. I've tried playing with accessing the key of the dictionary from the 2nd method in different ways, with converting the booking id into str differently, but all in all I get this mistake.

And I simply see no workaround since this code is already as simple as possible, I do everything step by step...
Reply
#2
So I finally found out the reason:


In the first block. No reason to convert dict to str:

result_auth_cookie_token = str ({'Cookie': adding_token_to_auth_token_value})
I had to call not the key of the dict from the method, but the method itself as only it could be considered as cookie by the function below from my general_http_methods file:

def general_put_method(url, cookies, body):
            result = requests.put(url, headers=headers, cookies=cookies, json=body)
            return result
Reply
#3
body strings?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,530 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,170 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,253 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,391 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Error "list indices must be integers or slices, not str" dee 2 1,461 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,393 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,868 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,578 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,363 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  matrix number assignement to the random indices juniorcoder 4 1,919 Feb-19-2022, 02:18 PM
Last Post: juniorcoder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020