【发布时间】:2019-01-18 06:59:04
【问题描述】:
编辑:抱歉,这一天很长。我以为我在描述中添加了更多信息。这是我的代码:
import requests
import json
url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21'
headers = {'Authorization' : ('username', 'password'), 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post(url, data=open('tweets.json', 'rb'), headers=headers)
这是我收到的错误,我在正确的目录中,并且 100% 有一个名为 tweets.json 的文件。
FileNotFoundError: [Errno 2] No such file or directory: 'tweets.json'
我正在尝试使用 Python 包的 Requests 向 IBM Watson 写入 API 请求。我已经尝试了几个小时,但没有运气。
我已经成功地在 cURL 中为 api 写了一个 POST,但我真的很难将它导入我的应用程序所在的语言 python。
谁能帮我把这个 cURL 请求翻译成 Python 请求?
curl -X POST -u "password:username" --header "Content-Type: application/json" --data-binary @tone.json "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21"
最好, 瑞恩
【问题讨论】:
-
您能解释一下您面临的问题吗? requests documentation 相当简单,并且您没有使用任何异常卷曲。 Stack Overflow 不是代码编写服务,您需要详细说明您面临的具体问题。
-
这篇文章应该回答你的问题:stackoverflow.com/questions/28259697>
标签: python-3.x curl python-requests