Python请求负载而不排序



我试图使用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

字典中也有两次selectedUser,所以这行不通,因为你只会有你分配的最后一个键/值对,不确定正确的格式是什么但是元组允许你重复一个键:

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 body键的顺序。如果您只打算使用selectedUser一次,那么删除您不打算发布的内容。

相关内容

  • 没有找到相关文章

最新更新