1

如何在请求中发布有序参数?

我尝试了以下方法,但它们都不起作用:

payload = dict{('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e':8,'data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')}

payload = OrderedDict{('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e':8,'data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')}

payload = (('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e':8,'data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15'))

payload = ([('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e':8,'data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')])

我得到的错误是:

SyntaxError: invalid syntax

这一项以随机顺序发布参数,没有错误:

payload = {'f':'1','s':'2','t':'3','f':'4','ft':'5','s':'6','se':'7','e':8,'data[1]':'9','t':'10','el':'1q','data[2]':'12','data[3]':'13','data[4]':'14','htmldata[5]':'15'}

如何使用以下代码发布有序参数?

c = requests.post(url, params = payload)
4

1 回答 1

4
  • dict{...}错了,应该是dict(...)。同样适用OrderedDict{...}
  • dict并将OrderedDict序列作为参数
  • ('e':8,'data[1]','9')在你的元组列表中。应该是('e',8),('data[1]','9')

这会产生一个dict(它等同于您发布的工作 dict 文字),它将始终是无序的:

payload = dict([('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e', 8),('data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')])

这会产生一个元组的元组请求不会将其作为参数data

payload = (('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e', 8),('data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15'))

剩下的两个(有序字典元组列表)将产生你想要的:

from collections import OrderedDict
payload = OrderedDict([('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e',8),('data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')])
payload = [('f','1'),('s','2'),('t','3'),('f','4'),('ft','5'),('s','6'),('se','7'),('e', 8),('data[1]','9'),('t','10'),('el','1q'),('data[2]','12'),('data[3]','13'),('data[4]','14'),('htmldata[5]','15')]
于 2013-10-13T16:59:59.720 回答