Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coverting POST from requests to GAE urlfetch

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.

like image 814
David542 Avatar asked Feb 10 '23 22:02

David542


2 Answers

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)
like image 180
David542 Avatar answered Feb 13 '23 10:02

David542


Take a look at the https://github.com/paypal/PayPal-Python-SDK I managed to patch this lib easily to work with GAE as described here: https://github.com/paypal/PayPal-Python-SDK/issues/66

Requests works on GAE, but only version 2.3.0(!)

On Google Appengine (version 1.9.17) requests version 2.3.0 (only!) works IN PRODUCTION (but not on SDK) if you have billing enabled, which enables sockets support.

requests on the Appengine SDK fails with all https:// requests:

  ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))

requests version 2.4.1 fails with:

  File "distlib/requests/adapters.py", line 407, in send
    raise ConnectionError(err, request=request)
  ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))

requests version 2.5.1 fails with:

  File "distlib/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
  ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))

Info on sockets support: https://cloud.google.com/appengine/docs/python/sockets/

like image 35
cat Avatar answered Feb 13 '23 11:02

cat