Sejal Mohata
Sejal Mohata

Reputation: 23

Add parameters to a given URL in Python

I want to add the following parameters:

params= {'Context' : { "Country":"US", "Region":"US", "Language":"en", "Segment":"dhs", "CustomerSet":"19"}, 'itemIdentifiers' : ['210-amsr','320-9704']}

to the base URL = https://www.catalogue.com/getdetail?

so that the final URL looks like this: https://www.catalogue.com/getdetail?Context={"Country":"US","Region":"US","Language":"en","Segment":"dhs","CustomerSet":"19"}&itemIdentifiers=210-amsr,320-9704

I've tried the following approach: api_url = url+parse.urlencode(params, doseq=True)

but I end up with: https://www.catalogue.com/getdetail?Context=Country&Context=Region&Context=Language&Context=Segment&Context=CustomerSet&itemIdentifiers=210-amsr&itemIdentifiers=320-9704

Upvotes: 1

Views: 1034

Answers (1)

Dan
Dan

Reputation: 45741

The requests can handle this for you:

import requests

base_url = r"https://www.catalogue.com/getdetail"
params= {
    "Context" : { 
        "Country": "US", 
        "Region" : "US", 
        "Language" : "en", 
        "Segment" : "dhs", 
        "CustomerSet" : "19"
     }, 
     "itemIdentifiers" : ['210-amsr', '320-9704'],
}

requests.get(base_url, params=params)

Upvotes: 1

Related Questions