Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misskey-jsのAPIClient.requestでファイルがドライブにアップロードできない #15121

Open
1 task done
takaion opened this issue Dec 12, 2024 · 0 comments
Open
1 task done
Labels
🧩API Issues regarding the interface between the server and the client. 🐛Bug Unexpected behavior packages/misskey-js

Comments

@takaion
Copy link

takaion commented Dec 12, 2024

💡 Summary

APIClientrequestdrive/files/createを使うとファイルのアップロードに失敗します。
drive/files/createの場合はapi.tsでContent-Typemultipart/form-dataに指定していますがこうするとboundaryが入らないのでうまくアップロードできないようです。
次のようにheadersないしはContent-Typeの指定を外すとアップロードできます:

const payload = new FormData();
payload.append("i", "YOUR_TOKEN");
payload.append("file", new Blob(["test"]));
fetch("https://misskey.io/api/drive/files/create", {
    method: "POST",
    body: payload,
    credentials: "omit",
    cache: "no-cache"
})

解決方法の目処(fetchContent-Typeを渡さない)も立っているのでこの方針で問題なさそうでしたらこちらからPRを送ることもできます。

🥰 Expected Behavior

request("drive/files/create", params)でファイルがアップロードできる

🤬 Actual Behavior

必ず次のエラーが発生します

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5682:19)
    at successSteps (node:internal/deps/undici/undici:5663:27)
    at fullyReadBody (node:internal/deps/undici/undici:4561:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async consumeBody (node:internal/deps/undici/undici:5672:7)
    at async file:///xxxxx/node_modules/misskey-js/built/api.js:64:50

デバッガでレスポンスを参照してみたところサーバーから本文なし(Content-Length: 0)の400 Bad Requestが返ってきていました
エラーは空の文字列をJSONとして解釈しようとしたために発生していますが根本的にはリクエストにあると見られます

📝 Steps to Reproduce

再現するための最小のTypeScriptコードは次のとおりです:

import { APIClient } from "misskey-js/api.js";

const api = new APIClient({origin: "https://misskey.io", credential: "YOUR_TOKEN"});
api.request("drive/files/create", {file: new Blob(["test"])});

💻 Frontend Environment

* Model and OS of the device(s): 自作PC、Ubuntu 22.04 LTS (WSL2 on Windows 10)
* Browser: none
* Server URL: misskey.io
* Misskey: 2024.11.0

* misskey-js: 2024.11.0
* Node.js v22.11.0

🛰 Backend Environment (for server admin)

* Installation Method or Hosting Service:
* Misskey:
* Node:
* PostgreSQL:
* Redis:
* OS and Architecture:

Do you want to address this bug yourself?

  • Yes, I will patch the bug myself and send a pull request
@takaion takaion added the ⚠️bug? This might be a bug label Dec 12, 2024
@samunohito samunohito added 🐛Bug Unexpected behavior 🧩API Issues regarding the interface between the server and the client. packages/misskey-js and removed ⚠️bug? This might be a bug labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧩API Issues regarding the interface between the server and the client. 🐛Bug Unexpected behavior packages/misskey-js
Projects
Development

No branches or pull requests

2 participants