Python 'requests' adding parameters in the wrong order

python, python-2.7, python-requests

Solution

Dictionaries have no fixed order. Pass in your parameters as a sequence of `(key, value)` pairs instead if you require ordered parameters:

qparams = (
    ('cli', 10),
    ('url', 'www.google.com'),
)

You should also leave off the `?` from the URL, `requests` will handle that for you.

Demo:

>>> import requests
>>> endpoint = 'http://data.alexa.com/data'
>>> qparams = (
...     ('cli', 10),
...     ('url', 'www.google.com'),
... )
>>> response = requests.get(endpoint, params=qparams)
>>> response.url
u'http://data.alexa.com/data?cli=10&url=www.google.com'
>>> print response.content
<?xml version="1.0" encoding="UTF-8"?>

<ALEXA VER="0.9" URL="google.com/" HOME="0" AID="=" IDN="google.com/">

<KEYWORDS>
<KEYWORD VAL="Mountain View"/>
</KEYWORDS><DMOZ>
<SITE BASE="google.com/" TITLE="Google" DESC="Enables users to search the world's information, including webpages, images, and videos. Offers unique features and search technology.">
<CATS>
<CAT ID="Top/Computers/Internet/Searching/Search_Engines/Google" TITLE="Search Engines/Google" CID="374822"/>
<CAT ID="Top/Regional/North_America/United_States/California/Localities/M/Mountain_View/Business_and_Economy/Industrial/Computers_and_Internet" TITLE="Industrial/Computers and Internet" CID="625367"/>
<CAT ID="Top/World/Français/Informatique/Internet/Recherche/Moteurs_de_recherche/Google" TITLE="Moteurs de recherche/Google" CID="247347"/>
<CAT ID="Top/World/Arabic/إقليمـي/الشرق_الأوسط/السعودية/تجارة_و_أقتصاد/كمبيوتر_و_إنترنت/محركات_بحث" TITLE="كمبيوتر و إنترنت/محركات بحث" CID="204954"/>
<CAT ID="Top/World/Español/Informática/Internet/Buscando_en_Internet/Motores_de_búsqueda" TITLE="Buscando en Internet/Motores de búsqueda" CID="29498"/>
</CATS>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="google.com/" TEXT="1" SOURCE="panel"/>
<REACH RANK="1"/>
<RANK DELTA="+0"/>
<COUNTRY CODE="US" NAME="United States" RANK="1"/>
</SD>
</ALEXA>

Problem

``` import requests endpoint = 'http://data.alexa.com/data?' qparams = {'cli': 10, 'url': 'www.google.com'} response = requests.get(endpoint, params=qparams) print response.url ``` This shows me that it looked at http://data.alexa.com/data?url=www.google.com&cli=10 Which is the wrong URL, it should be http://data.alexa.com/data?cli=10&url=www.google.com Can anyone help?

Original source