在Google云函数Python中获取请求体
我正在尝试在Python中的Google云函数上设置条带Webhook。但是,我在从函数获取请求主体时遇到了一个问题。请看一下我的表。代码如下 基本上,我如何获得在Google云函数Python中获取请求体,python,google-cloud-platform,google-cloud-functions,stripe-payments,Python,Google Cloud Platform,Google Cloud Functions,Stripe Payments,我正在尝试在Python中的Google云函数上设置条带Webhook。但是,我在从函数获取请求主体时遇到了一个问题。请看一下我的表。代码如下 基本上,我如何获得请求.body?这在云函数中是否以某种方式提供 import json import os import stripe from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt stripe.api_key = '
请求.body
?这在云函数中是否以某种方式提供
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)
请求
对象是的一个实例
根据您希望对请求正文执行的操作,您可以调用
request.args
,request.form
,request.files
,request.values
,request.json
,或者request.data
,如果请求主体未被解析。请求对象是的实例
根据您希望对请求正文执行的操作,您可以调用
request.args
,request.form
,request.files
,request.values
,request.json
,或request.data
,以防请求主体未被解析。如果无法解析其内容,则请求对象似乎不会包含完整的有效负载。看见body是一个nodejs构造,在python/Flask中不可用。答案取决于您如何发布或放置数据和mimetype。这个答案可能会有帮助:如何调用函数?您可以尝试使用request.get_data()
?如果无法解析请求对象的内容,则该对象看起来不会包含完整的负载。看见body是一个nodejs构造,在python/Flask中不可用。答案取决于您如何发布或放置数据和mimetype。这个答案可能会有帮助:如何调用函数?您可以尝试使用请求。获取数据()
?