
php小编苹果为您带来关于如何正确使用AnonFiles API发布文件的指南。AnonFiles API是一个强大的工具,可以帮助您快速、安全地上传和分享文件。本指南将为您提供详细的步骤和示例,帮助您轻松掌握API的使用方法。无论您是开发者还是普通用户,都能从本指南中获得实用的技巧和建议,让您的文件分享体验更加顺畅和高效。让我们一起来探索AnonFiles API的魅力吧!
我正在尝试创建一个函数,使用 anonfiles api 在 anonfiles.com 网站上托管您的文件。即使我正确使用了 api,它总是返回 nil。 响应缺少消息。
func host(file string) {
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("\033[1;31mCommand > Host: Could not read file,", err, "\033[0m")
return
}
url := "https://api.anonfiles.com/upload"
request, err := http.NewRequest("POST", url, bytes.NewBuffer(fileBytes))
if err != nil {
fmt.Println("\033[1;31mCommand > Host: Could not post request,", err, "\033[0m")
return
}
request.Header.Set("Content-Type", "application/octet-stream")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("\033[1;31mCommand > Host: Could not send request,", err, "\033[0m")
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("\033[1;31mCommand > Host: Could not read response,", err, "\033[0m")
return
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println("\033[1;31mCommand > Host: Could not parse response,", err, "\033[0m")
return
}
if response.StatusCode == 200 {
if result["url"] == nil {
fmt.Println("\033[1;31mCommand > Host: Response is missing URL\033[0m")
return
}
fmt.Println("File hosted successfully:", result["url"].(string))
} else {
if result["message"] == nil {
fmt.Println("\033[1;31mCommand > Host: Response is missing message\033[0m")
return
}
fmt.Println("\033[1;31mCommand > Host:\033[0m", result["message"].(string))
}
}
我想花点时间将这些评论扩展为答案。
首先,正如我们已经讨论过的,您没有使用正确的 api 来上传文件。如果我们修改您的代码以显示完整的响应正文,如下所示:
client := &http.client{}
response, err := client.do(request)
if err != nil {
fmt.println("\033[1;31mcommand > host: could not send request,", err, "\033[0m")
return
}
defer response.body.close()
body, err := ioutil.readall(response.body)
if err != nil {
fmt.println("\033[1;31mcommand > host: could not read response,", err, "\033[0m")
return
}
fmt.printf("body:\n%s\n", body)我们看到以下内容:
{
"status": false,
"error": {
"message": "no file chosen.",
"type": "error_file_not_provided",
"code": 10
}
}我们收到此错误是因为您没有在 multipart/form-data 请求中提供 file 参数。 我之前链接到的帖子有几个示例发送多部分请求;我已经测试了其中的几个,它们似乎按预期工作。
您还对 api 返回的响应做出了错误的假设。如果我们使用 curl 发出成功的请求并捕获响应 json,我们会发现它如下所示:
{
"status": true,
"data": {
"file": {
"url": {
"full": "https://anonfiles.com/k8cdobwey7/test_txt",
"short": "https://anonfiles.com/k8cdobwey7"
},
"metadata": {
"id": "k8cdobwey7",
"name": "test.txt",
"size": {
"bytes": 12,
"readable": "12 b"
}
}
}
}
}请注意,没有 response["url"] 或 response["message"]。如果您想要上传文件的url,则需要获取response["data"]["file"]["url"]["full"](或["short"])。
同样,我们可以看到上面的错误响应示例,如下所示:
{
"status": false,
"error": {
"message": "no file chosen.",
"type": "error_file_not_provided",
"code": 10
}
}这不是 result["message"];那是 result["error"]["message"]。
因为您要解组到 map[string] 接口 ,所以获取这些嵌套键会有点痛苦。我发现为上述响应创建 go 结构是最简单的,只需将其解组为适当类型的变量即可。
这让我得到以下类型:
type (
anonfilesurl struct {
full string `json:"full"`
short string `json:"short"`
}
anonfilesmetadata struct {
id string `json:"id"`
name string `json:"name"`
size struct {
bytes int `json:"bytes"`
readable string `json:"readable"`
} `json:"size"`
}
anonfilesdata struct {
file struct {
url anonfilesurl `json:"url"`
metadata anonfilesmetadata `json:"metadata"`
} `json:"file"`
}
anonfileserror struct {
message string
type string
code int
}
anonfilesresponse struct {
status bool `json:"status"`
data anonfilesdata `json:"data"`
error anonfileserror `json:"error"`
}
)然后解组响应如下所示:
var result anonfilesresponse err = json.unmarshal(body, &result)
我们可以请求以下字段:
fmt.Printf("URL: %s\n", result.Data.File.URL.Full)以上就是如何正确使用AnonFiles API发布文件?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号