Lambda函数在IoT Analytics中触发,该函数向外部API发送POST请求



我正在尝试在Python中设置AWS Lambda函数,该函数在IOT Analytics处理过程中触发,并向外部API发送两个连续的POST请求(如果满足条件(。我无法直接导入"请求"包,因为它太大,无法进行内联编辑,所以我已经将我的Python脚本和包上传到zip文件中,然后将其上传到AWS。

在运行lambda时,我会在请求包文件中遇到错误,但我并不真正理解这些错误。我也不确定如何返回API响应,因为我在响应中得到了一个序列化错误。这是我的lambda代码:

import json
import os
import time
import requests
def lambda_handler(event,context):
headers = {
"authorization": "Basic XXXXXXXXX",
"content_type": "application/json"
} #API request header
url = "https://example.com/path" #API request URL
msgs = json.loads(json.dumps(event))
for msg in msgs:
deviceID = msg["deviceID"]
data = msg["data"]
if (condition over received message):
body1 = {
"paramAA": paramAA,
"paramB": [{
"paramBA": paramBA1,
"paramBB": paramBB
}]
}
response_1 = requests.post(url,headers=headers,data=body1) #First API request
time.sleep(600)
body2 = {
"paramAA": paramAA,
"paramB": [{
"paramBA": paramBA2,
"paramBB": paramBB,
}]
}
response_2 = requests.post(url,headers=headers,data=body2) #Second API request
else:
pass
else:
pass
return {
'statusCode': 200,
'url' : url,
'response_1.code' : response_1.status_code,
'response_1_msg' : response_1.text,
'response_2.code' : response_2.status_code,
'response_2_msg' : response_2.text
}

你知道如何纠正这些错误吗?

如果我用"reponse_1:json.dumps(response1("更改返回,我会得到以下错误(无论是zip包还是SDK(:

{
"errorMessage": "Object of type Response is not JSON serializable",
"errorType": "TypeError",
"stackTrace": [
"  File "/var/task/lambda_function.py", line 56, in lambda_handlern    'response_1' : json.dumps(response_1),n",
"  File "/var/lang/lib/python3.8/json/__init__.py", line 231, in dumpsn    return _default_encoder.encode(obj)n",
"  File "/var/lang/lib/python3.8/json/encoder.py", line 199, in encoden    chunks = self.iterencode(o, _one_shot=True)n",
"  File "/var/lang/lib/python3.8/json/encoder.py", line 257, in iterencoden    return _iterencode(o, 0)n",
"  File "/var/lang/lib/python3.8/json/encoder.py", line 179, in defaultn    raise TypeError(f'Object of type {o.__class__.__name__} 'n"

json库似乎可以转换为仅字符串的标准类型,但我不得不对更复杂的对象使用pickle。我相应地修改了我的代码,结果出现了utf-8编码错误:

"errorMessage": "Unable to marshal response: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte"

这是我现在拥有的脚本:

import pickle
import os
import time
import requests
def lambda_handler(event,context):
headers = {
"authorization": "Basic XXXXXXXXX",
"content_type": "application/json"
} #API request header
url = "https://example.com/path" #API request URL
msgs = pickle.loads(pickle.dumps(event))
for msg in msgs:
deviceID = msg["deviceID"]
data = msg["data"]
if (condition over received message):
body1 = {
"paramAA": paramAA,
"paramB": [{
"paramBA": paramBA1,
"paramBB": paramBB
}]
}
response_1 = requests.post(url,headers=headers,data=pickle.dumps(body1)) #First API request
exec_verif = "request 1 sent"
time.sleep(600)
body2 = {
"paramAA": paramAA,
"paramB": [{
"paramBA": paramBA2,
"paramBB": paramBB,
}]
}
response_2 = requests.post(url,headers=headers,data=pickle.dumps(body2)) #Second API request
exec_verif = "request 2 sent"
else:
response_1 = "fail1"
response_2 = "fail1"
else:
response_1 = "fail2"
response_2 = "fail2"
return {
'statusCode': 200,
'exec_verif' : exec_verif,
'response_1' : pickle.dumps(response_1),
'response_2' : pickle.dumps(response_2)
}

我感到奇怪的是,如果我在返回中不包括response_1和response_2,则函数将被执行并返回200状态,'exec_verif'为"已发送请求2",而没有对外部API的实际调用。API日志中没有任何调用的痕迹,即使调用失败。

使用base64编码对我很有用。这里给出了一个例子;https://docs.aws.amazon.com/apigateway/latest/developerguide/lambda-proxy-binary-media.html

更多详细信息请点击此处;https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html

相关内容

  • 没有找到相关文章