使用python中的AWS Lambda将Post请求发送到外部API [英] Send Post request to an external API using AWS Lambda in python
问题描述
我想向外部API发送帖子请求( https://example.com/api / jobs / test )每小时。
I want to send a post request to an external API (https://example.com/api/jobs/test) every hour.
我使用的Lambda函数如下:
The Lambda Function that I used is as follows:
Handler: index.lambda_handler
python: 3.6
index.py
import requests
def lambda_handler(event, context):
url="https://example.com/api/jobs/test"
response = requests.post(url)
print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP
测试事件:
{
"url": "https://example.com/api/jobs/test"
}
错误:
START RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Version: $LATEST
Unable to import module 'index': No module named 'requests'
END RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d
REPORT RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Duration: 0.65 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
任何帮助都将不胜感激。
Any help would be appreciated.
推荐答案
您需要将 requests
模块安装到项目目录并创建一个lambda部署包。请参阅此链接了解详细信息。
You need to install requests
module to your project directory and create a lambda deployment package. See this link for details.
简而言之,您需要在开发系统(PC或Mac)上创建index.py文件,安装Python&点到那个系统;他们按照文档中的步骤操作。要创建lambda,请选择上传压缩选项,而不是编辑内联一个
In short, you need to create your index.py file on you development system (PC or mac), install Python & pip on that system; them follow the steps in the doc. To create lambda, choose the 'Upload zip' option instead of the 'Edit inline' one
这篇关于使用python中的AWS Lambda将Post请求发送到外部API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!