![]() |
| "TypeError: string indices must be integers, not 'str'" while not using any indices - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: "TypeError: string indices must be integers, not 'str'" while not using any indices (/thread-39400.html) |
"TypeError: string indices must be integers, not 'str'" while not using any indices - bul1t - Feb-11-2023 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]> Process finished with exit code 1My 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 resultPlease, 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... RE: "TypeError: string indices must be integers, not 'str'" while not using any indices - bul1t - Feb-11-2023 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 RE: "TypeError: string indices must be integers, not 'str'" while not using any indices - deanhystad - Feb-11-2023 body strings? |