从请求中隐藏POST到GAE urlfetch [英] Coverting POST from requests to GAE urlfetch

查看:97
本文介绍了从请求中隐藏POST到GAE urlfetch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PayPal进行付款。以下是它如何正确使用请求

  res =请求。 post(get_payment_info_url,headers = headers,data = params)
res_data = res.json()



<但是当我尝试使用 urlfetch 执行同样的请求时,它给了我一个错误(来自PayPal的200响应,但付款失败):

  res = urlfetch.fetch(url = make_payment_url,payload = params,method = urlfetch.POST,headers = headers)
res_data = json.loads(res)

{u'responseEnvelope':{u'timestamp':u'2015-02-15T23:21:52.729-08:00',u'ack':u '失败',u'build':u'15089777',u'correlationId':u'e202988541fde'},
''rorror':[{u'domain':u'PLATFORM',u'message' :u'Ivalid request:{0}',u'severity':u'Error',u'subdomain':
u'Application',u'category':u'Application',u'errorId': u'580001'}]}

似乎Google可能会剥离标题或其他内容?那么如果Google这样做,我会如何提出这个请求?



最后,是否有理由使用 urlfetch 请求(我已经将本地导入到我的GAE项目中了?请求似乎更加容易和友好使用。

解决方案

为此,有效负载需要被urlencoded。 $ c> res2 = urlfetch.fetch(
url,
headers = headers,
method ='POST',
payload = urllib.urlencode(params)

res2_data = json.loads(res2.content)


I am doing a payment with PayPal. Here is how it works properly with requests:

res = requests.post(get_payment_info_url, headers=headers, data=params)
res_data = res.json()

But then when I try and do the same request with the urlfetch, it gives me an error (a 200 response from PayPal, but the payment Fails):

res = urlfetch.fetch(url=make_payment_url, payload=params, method=urlfetch.POST, headers=headers)
res_data = json.loads(res)

{u'responseEnvelope': {u'timestamp': u'2015-02-15T23:21:52.729-08:00', u'ack': u'Failure', u'build': u'15089777', u'correlationId': u'e202988541fde'}, 
u'error': [{u'domain': u'PLATFORM', u'message': u'Invalid request: {0}', u'severity': u'Error', u'subdomain': 
u'Application', u'category': u'Application', u'errorId': u'580001'}]}

It seems that maybe Google is stripping headers or something? How would I then make this request if Google is doing that?

Finally, is there any reason to use urlfetch over requests (which I've imported locally into my GAE project? Requests seems so much easier and 'friendly' to use.

解决方案

For this, the payload needs to be urlencoded. Here is what worked:

res2 = urlfetch.fetch(
                 url,
                 headers=headers,
                 method='POST',
                 payload=urllib.urlencode(params)
               )
res2_data = json.loads(res2.content)

这篇关于从请求中隐藏POST到GAE urlfetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆