完全符合文档要求(https://cndl.synology.cn/download/Document/Software/DeveloperGuide/Package/FileStation/All/enu/Synology_File_Station_API_Guide.pdf)。 我正在编写一个Android程序,其中包含将文件上传到filestation的功能。完全按照文档写的,但是服务器返回{"error": {"code": 101}, "success": false}。请参阅文档。错误101是指“没有API、方法或版本的参数”。但我用postman测试同样的参数和数据,是可以上传成功的。我使用rxhttp(https://github.com/liujingxing/rxhttp)来上传。以下是我的上传代码,请帮助我。
RxHttp.patchForm("http://myIp:port/webapi/entry.cgi")
.add("api", "SYNO.FileStation.Upload")
.add("method", "upload")
.add("version", 3)
.add("path", "/newFolder")
.add("create_parents", false)
.add("size", "file size")
.addPart(context, "file", "file path")
.upload(AndroidSchedulers.mainThread(), progress -> {
int currentProgress = progress.getProgress();
//The progress is displayed normally from 0-100, and then the server
//returns error 101
ALog.i(currentProgress);
})
.asString()
.subscribe(s -> {
ALog.i(s); //{"error":{"code":101},"success":false}
}, throwable -> {
ALog.i(throwable.getMessage());
});
我在文件站官方app上做了抓包测试,发现上传文件的参数中没有包含“content-length”,这是文件站app上传的文件
--5ed341e2-7d01-419a-bd7d-ecfd7cb1f1b3
Content-Disposition: form-data; name="api"
SYNO.FileStation.Upload
...
以下是我的代码运行后捕获的内容,多了一个“content-length”对比
--5ed341e2-7d01-419a-bd7d-ecfd7cb1f1b3
Content-Disposition: form-data; name="api"
Content-Length: 23
SYNO.FileStation.Upload
...
但我认为这不会影响上传,而且我还没有找到删除参数中“content-length”的方法。到目前为止,我猜测可能是“content-length”导致服务器无法识别参数值。请帮助我。
字段中的“Content-Type: XXX”导致请求错误。只需将它们全部删除即可。
之前:
...
--457a8fd49e934141ad0fe038af0ca3fe
Content-Type: XXX
Content-Disposition: form-data; name="api"
...
之后: ... --457a8fd49e934141ad0fe038af0ca3fe 内容处置:表单数据;名称=“API” ...