[英]Getting Request Body in Google Cloud Function Python
I'm trying to setup Stripe Webhooks on Google Cloud Functions in Python. However, I'm running into an issue with getting the request body from the function. Please have a look at my.我正在尝试在 Python 中的 Google Cloud Functions 上设置 Stripe Webhooks。但是,我在从 function 获取请求正文时遇到了问题。请查看我的。 code below.
下面的代码。
Essentially how can I get the request.body
?本质上我怎样才能得到
request.body
? Is this provided in Cloud Functions somehow?这是在 Cloud Functions 中以某种方式提供的吗?
import json
import os
import stripe
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
stripe.api_key = 'XXXX'
# Using Django
@csrf_exempt
def my_webhook_view(request):
payload = request.body # This doesn't work!
event = None
print(payload)
try:
event = stripe.Event.construct_from(
json.loads(payload), stripe.api_key
)
except ValueError as e:
# Invalid payload
return HttpResponse(status=400)
# Handle the event
if event.type == 'payment_intent.succeeded':
payment_intent = event.data.object # contains a stripe.PaymentIntent
# Then define and call a method to handle the successful payment intent.
# handle_payment_intent_succeeded(payment_intent)
print(payment_intent)
elif event.type == 'payment_method.attached':
payment_method = event.data.object # contains a stripe.PaymentMethod
# Then define and call a method to handle the successful attachment of a PaymentMethod.
# handle_payment_method_attached(payment_method)
# ... handle other event types
print(payment_intent)
else:
print('Unhandled event type {}'.format(event.type))
return HttpResponse(status=200)
The request
object is an instance of flask.Request
. request
object 是flask.Request
的一个实例。
Depending on what you want to do with the request body, you could call request.args
, request.form
, request.files
, request.values
, request.json
, or request.data
in the event that the request body hasn't been parsed.根据您想要对请求主体执行的操作,您可以调用
request.args
、 request.form
、 request.files
、 request.values
、 request.json
或request.data
在请求主体尚未解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.