将 POST 从请求覆盖到 GAE urlfetch

Dav*_*542 3 python google-app-engine multithreading python-requests

我正在使用 PayPal 付款。以下是它如何正常工作requests

res = requests.post(get_payment_info_url, headers=headers, data=params)
res_data = res.json()
Run Code Online (Sandbox Code Playgroud)

但是当我尝试对 执行相同的请求时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'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'}]}
Run Code Online (Sandbox Code Playgroud)

似乎谷歌正在剥离标题之类的?如果谷歌这样做,我将如何提出这个请求?

最后,是否有任何理由urlfetch过度使用requests(我已在本地将其导入到我的 GAE 项目中?使用请求似乎更容易和“友好”。

Dav*_*542 5

为此,需要对有效负载进行 urlencoded。这是有效的:

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