[英]How to take data from a .txt file and save it in a .bat variable?
[英]How can i take data one line at a time from a txt file and use it as a variable to send requests?
我目前正在做一個我認為很酷的小項目。 但是,我遇到了很大的障礙。 我試圖讓它從文本文件中讀取用戶名並將其上傳到 API,但是,我不斷收到錯誤代碼 400,服務器響應表明請求中的用戶名字段為空。 我還添加了一個打印語句來確認憑證變量設置正確,這讓我認為這是我與 API 的通信錯誤。我認為另一個問題可能是我將內容類型設置為“純文本” . 我運行 Wireshark 查看我提出的確切請求,看起來所有內容都被壓縮在一起。 這是我當前的代碼:
import requests
import time
with open('./user.txt', 'r') as f:
creds = f.read()
print(f"Current User: {creds}")
url = 'http://example.com/api'
data = {f"nickname":"{creds}","password":"{creds}","email":"","referral":"null"}
headers = {'Content-Type': 'text/plain'}
response = requests.post(url, data=data, headers=headers)
我對使用請求庫還很陌生,所以如果這是一個糟糕的問題,我很抱歉,但我們將不勝感激任何幫助。
Wireshark 聲明我的請求數據是nickname=%7Bcreds%7D&password=%7Bcreds%7D
。
import time
import requests
import json
with open('./user.txt', 'r') as f:
for line in f:
creds = line.strip()
print(f"Current User: {creds}")
url = 'http://example.com/api'
data = {f"nickname":"{creds}","password":"{creds}","email":"","referral":"null"}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(data), headers=headers)
print(response.status_code)
print(response.text)
問題出在我的 header 類型上! 我不需要使用JSON
或plain/text
。 我使用了application/x-www-form-urlencoding
,這是我從文件的歷史記錄中獲得的。 應用程序現在讀取每個用戶名和密碼,並將其發送到服務器!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.