我正在尝试实现PayPal IPN功能.基本协议如下:
当我尝试urllib.urlencode PayPal发送给我的params时,我得到一个:
While calling send_response_to_paypal. Traceback (most recent call last):
File "<snip>/account/paypal/views.py", line 108, in process_paypal_ipn
verify_result = send_response_to_paypal(params)
File "<snip>/account/paypal/views.py", line 41, in send_response_to_paypal
params = urllib.urlencode(params)
File "/usr/local/lib/python2.6/urllib.py", line 1261, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我知道urlencode执行ASCII编码,在某些情况下,用户的联系信息可以包含非ASCII字符.这是可以理解的.我的问题是,如何使用urllib2.urlopen(req)(或其他方法)将非ASCII字符编码为POST到URL
细节:
我在PayPal的原始请求中读取了以下内容(GET用于测试):
def read_ipn_params(request):
if request.POST:
params= request.POST.copy()
if "ipn_auth" in request.GET:
params["ipn_auth"]=request.GET["ipn_auth"]
return params
else:
return request.GET.copy()
Run Code Online (Sandbox Code Playgroud)
我用来从处理页面向PayPal发回请求的代码是:
def send_response_to_paypal(params):
params['cmd']='_notify-validate'
params …Run Code Online (Sandbox Code Playgroud)