2

我正在尝试使用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
4

1 回答 1

1

您可以传递元组来代替 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

此外,您的dictselectedUser中有两次因此无法工作,因为您将只有分配的最后一个键/值对,不确定正确的格式是什么,但元组再次允许您重复一个键:

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

您可以看到使用 dict 给您留下的只是selectedUser=1分配的一对中的最后一个。这实际上可能是您的问题,而不是帖子正文键的顺序。如果您只想使用selectedUser一次,请删除您不想发布的内容。

于 2016-10-05T19:55:30.240 回答