ファイルを読み込む(RAW)
ストリーミング読み込み
-
API の説明
GET /read_file?filename=[URL エンコード済みファイル名] HTTP/1.1- 省略可能なクエリパラメータ
md5=1:レスポンスヘッダーにContent-MD5(Base64)を追加します
- 省略可能なクエリパラメータ
-
想定されるレスポンス
- 成功:HTTP 200。レスポンスボディはファイル内容のバイトストリームです(base64 エンコードなし)
- 失敗:
- HTTP 404 +
{"message":"File not found"}(ファイルが存在しない、または filename が指定されていない場合) - HTTP 415 +
{"message":"File is a directory"}(リクエストされたパスがディレクトリの場合)
- HTTP 404 +
-
API を使用してデバイス上のスクリプトファイルをストリーミングで読み込む Python 3.x の例(requests モジュールが必要):
# -*- coding: utf-8 -*-
import requests
with requests.get(
"http://192.168.31.72:46952/read_file",
params={"filename": "lua/scripts/123.lua", "md5": 1},
stream=True,
timeout=30,
) as r:
r.raise_for_status()
with open("r:/123.lua", "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print("Content-MD5:", r.headers.get("Content-MD5"))
Base64 による読み込み
-
API の説明
POST /read_file HTTP/1.1
{"filename":"ファイル名"} -
想定されるレスポンス
{"code":0,"message":"操作成功","data":"ファイル内容の base64 エンコード文字列"}{"code":4,"message":"无法读取文件"}{"code":8,"message":"参数错误"} -
API を使用してデバイスからスクリプトファイルを読み込む Python 3.x の例(requests モジュールが必要):
# -*- coding: utf-8 -*-
import base64
import requests
resp = requests.post(
"http://192.168.31.72:46952/read_file",
json={"filename": "lua/scripts/123.lua"},
timeout=30,
)
print(resp.status_code, resp.reason) # 成功時は 200 OK
if resp.status_code == 200:
ret = resp.json()
print(ret.get("message"))
if ret.get("code") == 0:
print(base64.b64decode(ret["data"]).decode("utf-8")) # base64 データをバイト列にデコードし、UTF-8 文字列へ明示的に変換する
else:
print(resp.text)
補足
- この API でファイル操作の基準となるルートディレクトリは
/var/mobile/Media/1ferver/です - ストリーミング読み込み API は
2025-07-11以降のバージョンで利用できます