json - 由 Pubsub 触发的云函数
问题描述
import base64
import logging
def hello_pubsub(event, context):
"""Background Cloud Function to be triggered by Pub/Sub.
Args:
event (dict): The dictionary with data specific to this type of
event. The `data` field contains the PubsubMessage message. The
`attributes` field will contain custom attributes if there are any.
context (google.cloud.functions.Context): The Cloud Functions event
metadata. The `event_id` field contains the Pub/Sub message ID. The
`timestamp` field contains the publish time.
"""
import base64
import time
import json
import requests
print(event)
print("""This Function was triggered by messageId {} published at {}
""".format(context.event_id, context.timestamp))
if 'data' in event:
name = base64.b64decode(event['data']).decode('utf-8')
else:
name = 'World'
print(name)
print(type(name))
print('Hello {}!'.format(name))
payload = json.loads(name)
logging.debug(payload)
我试图执行这是云功能,但我收到一个错误,这可能是因为 json.loads()。如何将有效负载作为 Json
错误:
line 32, in hello_pubsub payload = json.loads(name) File "/opt/python3.8/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/opt/python3.8/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/opt/python3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
解决方案
推荐阅读
- c# - 如何实现空类型的泛型接口?
- java - 如何在java中获取日期时间的整数值?
- sql - SQLDeveloper:如果不存在则插入
- http - 为 Jboss 管理控制台 URL 配置 HTTP 标头
- discord.js - 无法读取未定义的属性“执行”
- java - 58P01 错误:无法打开文件“.\src\main\resources\data\court.csv”进行阅读:没有这样的文件或目录
- c# - 使用启动
vs UseStartup(nameof(YourAssembly)) - javascript - 是否可以在 VueJS(Quasar Framework)中跨页面使用变量?
- reactjs - 关于在 useEffect() 回调中附加处理程序时丢失事件的风险
- php - 如何在 php 数据库中插入 csv 文件的第一行?