[英]Using Python-Requests Library to Post a Text File
嗨,我在使用Python請求庫( http://docs.python-requests.org/en/latest/index.html )發布文本文件時遇到麻煩,您能告訴我我做錯了什么嗎?
我嘗試搜索相關問題,並從Python腳本中使用POST找到了此發送文件 ,但未回答我的問題。
這是我的代碼:
import codecs
import requests
# Create a text file
savedTextFile = codecs.open('mytextfile.txt', 'w', 'UTF-8')
# Add some text to it
savedTextFile.write("line one text for example\n and line two text for example")
# Post the file THIS IS WHERE I GET REALLY TRIPPED UP
myPostRequest = requests.post("https://someURL.com", files=savedTextFile)
我已經嘗試了上述幾種變體,但是我什么都沒得到(每次都會出現新錯誤)。 如何發布剛剛創建的txt文件? 我嘗試發布的API需要將文本文件發布到該API。
任何幫助表示贊賞!
files參數需要一個與文件處理程序匹配的文件名字典。 在源代碼中對此進行了描述(當前為第69行):
Github來源(requests / models.py)
#: Dictionary of files to multipart upload (``{filename: content}``).
self.files = files
有時最好的文檔是代碼。
您的最后一行應如下所示:
myPostRequest = requests.post("https://someURL.com", files={'mytextfile.txt': savedTextFile})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.