[英]how to urlencode a value that is a python dictionary with encoded unicode characters
I'm trying to make a url-encoded web request in python 2.7 where I want to send a list of python dictionaries that would be on the server decoded as a list of JSON objects. 我正在尝试在python 2.7中发出一个URL编码的Web请求,我想发送一个将在服务器上解码为JSON对象列表的python字典列表。 In essence I'm making:
本质上,我在做:
>>>urllib.urlencode({"param":"val", "items":[item1, item2] }, True)
where item1
can be something like { "a": u"š".encode("utf8") }
(simplified for the example) 其中
item1
可以像{ "a": u"š".encode("utf8") }
(在示例中已简化)
The problem arises because of the unicode characters. 由于unicode字符而出现问题。
If an item1
is by itself encoded, you get something meaningful: 如果对
item1
本身进行了编码,那么您将获得一些有意义的信息:
>>>urllib.urlencode(item1)
'a=%C5%A1'
however, if I call urllib.urlencode({"test": item1})
I get a mess: 但是,如果我调用
urllib.urlencode({"test": item1})
我会一团糟:
'test=%7B%27a%27%3A+%27%5Cxc5%5Cxa1%27%7D'
In this case, the unicode character is no longer encoded as %C5%A1
but as a longer sequence that is then incorrectly decoded on the server side. 在这种情况下,Unicode字符不再被编码为
%C5%A1
而是被编码为更长的序列,然后在服务器端被错误地解码。
Does anybody have a suggestion how to properly transform complex dictionary values (ie item1
) before calling urlencode
to avoid this issue? 是否有人建议在调用
urlencode
之前如何正确转换复杂的字典值(即item1
)以避免此问题?
One way or another you need to decode anything that was encoded before re-encoding Here is one approach: 在重新编码之前,您需要先解码一种已编码的内容,这是一种方法:
dictionary = {"test": item1}
urllib.urlencode(dict([(k, decode_operation(v)) for k, v in dictionary.iteritems()]))
I solved my problem by first calling json.dumps(item) for each item in item list and then calling urllib.urlencode. 通过首先为项目列表中的每个项目调用json.dumps(item),然后调用urllib.urlencode,解决了我的问题。
In short: 简而言之:
>>>urllib.urlencode({"param":"val", "items":[json.dumps(item) for item in items] }, True)
This solved the problem because the unicode characters are then encode using the \\uXXXX which can be on the server side properly decoded with a json parser. 这解决了问题,因为然后使用\\ uXXXX对Unicode字符进行编码,可以在服务器端使用json解析器对其进行正确解码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.