[英]How to properly do an AWS Lambda API Post Request with JSON file using Python
I wanted to confirm/see if there is a better way to be making a post request to an API endpoint that was generated for an AWS Lambda?我想确认/查看是否有更好的方法向为 AWS Lambda 生成的 API 端点发出发布请求? Simply I'm trying to optimize this curl without using a subprocess call.只是我试图在不使用子进程调用的情况下优化此卷曲。 With this code I get an error status code of 400.使用此代码,我得到 400 的错误状态代码。
Code I'm trying to optimize我正在尝试优化的代码
$ curl -X POST -d @test.json -H "x-api-key: {API_KEY}" {URL}
Python script I've created:我创建的 Python 脚本:
import requests
URL = "some_url"
API_KEY = "some_api_key"
headers = {'x-api-key': API_KEY}
r = requests.post(URL, headers=headers, json=test.json)
print(r.status_code)
print(r.json())
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
import requests
import json
URL = "some_url"
API_KEY = "some_api_key"
headers = {'x-api-key': API_KEY}
with open("test.json") as f:
data = json.load(f)
r = requests.post(URL, headers=headers, json=data)
print(r.status_code)
print(r.json())
This gives a status code of 200 & the proper JSON response.这给出了 200 的状态代码和正确的 JSON 响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.