【问题标题】:How do you update DynamoDB from Jupyter Notebook via API Gateway?如何通过 API Gateway 从 Jupyter Notebook 更新 DynamoDB?
【发布时间】:2020-07-18 05:33:41
【问题描述】:

我正在构建个人网络抓取工具,但仍处于开发阶段,但我想开始保存数据。问题是,我无法从笔记本级别 PUT 或 POST,这也意味着我无法遍历大量字典/json 对象。

但是,我可以通过 Postman 手动粘贴正文并发送。

下面是我的代码,目前返回:

pastebin URL 是:{“message”:“无法将请求正文解析为 json:无法识别的令牌 'ID':在 [Source: ( byte[])"ID=0&District=London"; 行:1,列:4]"}

import requests 

# defining the api-endpoint 
API_ENDPOINT = ##############

# data to be sent to api 
body = {
  "ID": 0,
  "District": "London"
}

# sending post request and saving response as response object 
r = requests.post(url = API_ENDPOINT, data = body) 

# extracting response text 
pastebin_url = r.text 
print("The pastebin URL is:%s"%pastebin_url) 

相关问题 - 我可以在这里使用 urllib 代替 requests 吗?

【问题讨论】:

  • 您能告诉我们API_ENDPOINT 指的是什么吗?
  • 这是一个 API 网关端点:https://xxxxxxxxx.execute-api.eu-west-2.amazonaws.com/default/foo_put

标签: python amazon-web-services request amazon-dynamodb aws-api-gateway


【解决方案1】:

您可以尝试以下方法:

r = requests.post(url = API_ENDPOINT, json = body) 

import json
r = requests.post(url = API_ENDPOINT,  headers={"Content-Type":"application/json"}, data = json.dumps(body)) 

【讨论】:

  • 非常感谢!第一个成功了!使用 urllib 而不是 requests 会是什么样子?
  • @mad_edge 没问题。但是,如果您已经有请求,为什么不使用它呢?
  • 这是因为 requests 在 AWS Lambda 中不能作为导入使用,它需要作为层包含,不像 urllib
  • @mad_edge 例如here
猜你喜欢
  • 1970-01-01
  • 2020-05-08
  • 2020-07-20
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 2023-02-03
相关资源
最近更新 更多