0

I have a request payload where i need to use a variable to get details from different products

payload = f"{\"filter\":\"1202-1795\",\"bpId\":\"\",\"hashedAgentId\":\"\",\"defaultCurrencyISO\":\"pt-PT\",\"regionId\":2001,\"tenantId\":1,\"homeRegionCurrencyUID\":48}"

i need to change the \"filter\":\"1202-1795\" to \"filter\":\"{variable}\" to populate the requests and get the info, but i'm struggling hard with the backslash in the f string

I tried to change for double to single quotes both inside the string and the opening and closing quotes, tried double {} and nothing works

this is my variable to populate the request

variable = ['1214-2291','1202-1823','1202-1795','1202-1742','1202-1719','1214-2000','1202-1198','1202-1090']
  • 1
    **Do not** generate JSON data using string formatting! Firstly construct data in dictionary and use [`json.dumps()`](https://docs.python.org/3/library/json.html#json.dumps) after. [Sample](https://tio.run/##VY9BT8MwDIXv@RWRT63UobaAGJN2QHDpCTTgjLLFW4PaJHJcpGrab@@y0Ujgm7/3nvXsR26dvV16mibTe0csv4OzQvwoMmrboVxLqOqyXlQPj/cgvBo7p3SkRyHjwN50jAQrmQLFL9/6RkcKMO@tCi3qpwNa/i9o3Kuh4@eBCO1ubN5fL6rnxdtHshAejLPXWF2W1UwZrZqPJdS6HjdXczr32bxE/W4pTqn6V2CK9S9f3uih9yGbhUIG9IoUOwrrDAooJKwgz4UnYzn7E8@n6Qw). – Olvin Roght Nov 01 '21 at 18:50

1 Answers1

1

Create a dict, loop over items in the list, set filter key, make the request.

import requests
payload = {"bpId":"",
           "hashedAgentId":"",
           "defaultCurrencyISO":"pt-PT",
           "regionId":2001,
           "tenantId":1,
           "homeRegionCurrencyUID":48}

items = ['1214-2291','1202-1823','1202-1795','1202-1742','1202-1719','1214-2000','1202-1198','1202-1090']
for item  in items:
    payload['filter'] = item
    response = requests.get(url, json=payload)

You can check the difference between data and json parameters in python requests package

buran
  • 13,682
  • 10
  • 36
  • 61