简体   繁体   English

在谷歌云中获取请求体 Function Python

[英]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.argsrequest.formrequest.filesrequest.valuesrequest.jsonrequest.data在请求主体尚未解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌云功能 - SendGrid - 400 错误请求 - Google Cloud Function - SendGrid - 400 Bad Request 从谷歌云返回的错误请求 function - Bad request returned from google cloud function 向 Google Cloud 发出经过身份验证的请求 Function - Making authenticated request to Google Cloud Function Firebase 云 function request.body 为空或未定义 - Firebase Cloud function request.body empty or undefined Python 脚本 http 请求在本地工作,但在作为谷歌云测试时不工作 Function - Python script http request working locally, but not when tested as a Google Cloud Function 在 Python Google Cloud Function 中键入提示? - type hints in a Python Google Cloud Function? 谷歌云 function python 无法与 blob 交互 - Google cloud function python cannot interact with blobs 谷歌云 Function 返回“错误:无法处理请求” - Google Cloud Function returning "Error: could not handle the request" 为什么我的 Google Cloud Function 中出现 python ArrtibuteError,阻止部署但我的代码在本地运行良好? - Why am I getting a python ArrtibuteError in my Google Cloud Function, preventing deployment but my code runs fine locally? 使用 Python 和 SQLAlchemy 从谷歌云 Function 连接到云 SQL - Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM