python 请求负载而不排序

python request payload without sorting

我正在尝试使用 python 请求模块通过以下代码将少量值传递给服务器,但在 http 请求中它改变了负载值的顺序。

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)

使用浏览器我可以看到值按以下顺序传递,但是当我使用 python 它改变顺序时,有什么办法可以避免这种排序

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

您可以传递元组代替字典:

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

此外,您的 dict 中有两次 selectedUser,所以这不会起作用,因为您只会分配最后一个 key/value 对,而不是确定正确的格式是什么,但元组将允许您重复一个键:

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

您可以看到使用字典只剩下 selectedUser=1,因为这是分配的最后一对。这实际上可能是您的问题,而不是 post 正文键的顺序。如果您只想使用 selectedUser 一次,请删除您不想使用的 post.