user19422024
user19422024

Reputation:

Passing variable in python request URL not working

def get_prod():
    name = input('Search Query:\n')
    params = {
    'page': '1',
    'perPage': '20',
    'query': name,
}
    response = requests.get('https:///api/public/search', params=params, headers=headers)
    response = response.json()
    id = response['results'][0]['id']
    print(id)
    prod_data = requests.get('https:///api/public/products/{id}', headers=headers)
    print(prod_data.text)

get_prod()

from the public API Search I am returning a valid ID but when the prod_data get request returns the error {"message":"Couldn't find Product with 'id'={id} [WHERE \"products\".\"deleted_at\" IS NULL]"} implying to me that it's not using that variable I can print. When I put a standard finite ID the request works but I'm sure why this won't

Upvotes: 0

Views: 457

Answers (1)

Dunura Dulshan
Dunura Dulshan

Reputation: 61

Seems like the variable value is not getting printed. Unless I'm missing something.

You need a f before the string if you're going with this method; f-strings.

prod_data = requests.get(f'https:///api/public/products/{id}', headers=headers)

These might help: https://matthew-brett.github.io/teaching/string_formatting.html

Upvotes: 2

Related Questions