Search code examples
pythonpython-requestsunify

python request payload without sorting


I am trying to use python request module to pass few values to server using the following code, but in the http request its changing the order of the payload values.

payload3 = {'OSILA_LinkTo_Site': OSILA_LinkTo_Site, 'OSILA_LinkTo_Service': OSILA_LinkTo_Service, 'OSILA_Locale': OSILA_Locale, 'OSILA_Locale_display': OSILA_Locale, 'OSILA_TimeZone': OSILA_TimeZone, 'OSILA_TimeZone_display': OSILA_TimeZone, 'selectedUser': '', 'OSILA_LinkTo_User_display': '', 'OSILA_LinkTo_User': OSILA_LinkTo_User, 'OSILA_DisplayName': OSILA_DisplayName, 'OSILA_CountryCode': OSILA_CountryCode, 'OSILA_AreaCode': OSILA_AreaCode, 'OSILA_LocalOfficeCode': OSILA_LocalOfficeCode, 'OSILA_LinkTo_Extension': OSILA_LinkTo_Extension, 'OSILA_ProductPackage': OSILA_ProductPackage, 'OSILA_ProductPackage_display': OSILA_ProductPackage, 'OSILA_DeviceType': OSILA_DeviceType, 'OSILA_DeviceType_display': OSILA_DeviceType, 'OSILA_Parameter_4': OSILA_Parameter_4, 'OSILA_Parameter_4_display': OSILA_Parameter_4, 'OSILA_Parameter_3': OSILA_Parameter_3, 'OSILA_Parameter_3_display': OSILA_Parameter_3, 'OSILA_Parameter_2': OSILA_Parameter_2, 'OSILA_Parameter_2_display': OSILA_Parameter_2, 'OSILA_Parameter_5': OSILA_Parameter_5, 'OSILA_Parameter_5_display': OSILA_Parameter_5, 'OSILA_Parameter_1': OSILA_Parameter_1, 'OSILA_Parameter_1_display': OSILA_Parameter_1, 'OSILA_CreateNewFax': OSILA_CreateNewFax, 'OSILA_LinkTo_ServiceFax': '', 'OSILA_CountryCodeFax': '', 'OSILA_AreaCodeFax': '', 'OSILA_LocalOfficeCodeFax': '', 'OSILA_LinkTo_ExtensionFax': '', 'OSILA_ProvisioningDate': OSILA_ProvisioningDate, 'OSILA_DateMail_1': OSILA_ProvisioningDate, 'OSILA_DateMail_2': '', 'OSILA_DateMail_3': '', 'OSILA_DateMail_4': '', 'OSILA_DateMail_5': '', 'OSILA_DateMail_6': '', 'OSILA_DateMail_7': '', 'OSILA_DateMail_8': '', 'OSILA_DateMail_9': '', 'attachEventListeners': '', 'webMgrRequestId': webMgrRequestId, 'WT': WT2, 'enterConfirm': 'true', 'users_R_0_C__select': 'on', 'selectedUser': '0'}

subcr = session.post("http://192.168.0.10:8080/OSILAManager/createSubscriber2.do", data=payload3)

using browser i can see the values are passing in the following order ,but when i use python its changing the order, is there any way to avoid this sorting

OSILA_LinkTo_Site
OSILA_LinkTo_Service
OSILA_Locale
OSILA_Locale_display
OSILA_TimeZone
OSILA_TimeZone_display
users_R_0_C__select
selectedUser
OSILA_LinkTo_User_display
OSILA_LinkTo_User
OSILA_DisplayName
OSILA_CountryCode
OSILA_AreaCode
OSILA_LocalOfficeCode
OSILA_LinkTo_Extension
OSILA_ProductPackage
OSILA_ProductPackage_display
OSILA_DeviceType
OSILA_DeviceType_display
OSILA_Parameter_4
OSILA_Parameter_4_display
OSILA_Parameter_3
OSILA_Parameter_3_display
OSILA_Parameter_2
OSILA_Parameter_2_display
OSILA_Parameter_5
OSILA_Parameter_5_display
OSILA_Parameter_1
OSILA_Parameter_1_display
OSILA_CreateNewFax
OSILA_LinkTo_ServiceFax
OSILA_CountryCodeFax
OSILA_AreaCodeFax
OSILA_LocalOfficeCodeFax
OSILA_LinkTo_ExtensionFax
OSILA_ProvisioningDate
OSILA_DateMail_1
OSILA_DateMail_2
OSILA_DateMail_3
OSILA_DateMail_4
OSILA_DateMail_5
OSILA_DateMail_6
OSILA_DateMail_7
OSILA_DateMail_8
OSILA_DateMail_9
attachEventListeners
webMgrRequestId
WT
enterConfirm

Solution

  • You can pass tuples in place of the dict:

    In [7]: 
       ...: import requests
       ...: d = {"foo":"bar","bar":"foo","123":"456"}
       ...: t = (("foo","bar"),("bar","foo"),("123","456"))
       ...: r1 = requests.post("http://httpbin.org", data=d)
       ...: r2 = requests.post("http://httpbin.org", data=t)
       ...: print(r1.request.body)
       ...: print(r2.request.body)
       ...: 
    bar=foo&123=456&foo=bar
    foo=bar&bar=foo&123=456
    

    Also you have selectedUser twice in your dict so that won't work as you will only have the last key/value pair you assign, not sure what the correct format is but again tuples would allow you to repeat a key:

    In [8]: import requests
       ...: d = {"foo":"bar","bar":"foo","123":"456","selectedUser":"0", "selectedUs
       ...: er":"1"}
       ...: t = (("foo","bar"),("bar","foo"),("123","456"), ("selectedUser","0"),("s
       ...: electedUser","1"))
       ...: r1 = requests.post("http://httpbin.org", data=d)
       ...: r2 = requests.post("http://httpbin.org", data=t)
       ...: print(r1.request.body)
       ...: print(r2.request.body)
       ...: 
    bar=foo&selectedUser=1&123=456&foo=bar
    foo=bar&bar=foo&123=456&selectedUser=0&selectedUser=1
    

    You can see using a dict leaves you with just selectedUser=1 as that is the last of the pair assigned. That may actually be your problem more than the order of the post body keys. If you only meant to use selectedUser once then remove whichever you did not mean to post.