这里有api,当发布形式为
的请求时,应该加载文件curl -X POST -d "url = http: //site.com/file.txt" http: // localhost: 8000 / submit
但是404退出了,这是什么原因? 或者如何通过POST在API中下载文件?
func downloadFile(url string) Task {
var task Task
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading")
}
defer resp.Body.Close()
filename := strings.Split(url, "/")[len(strings.Split(url, "/"))-1]
fmt.Println(filename)
out, err := os.Create(filename)
if err != nil {
fmt.Println("Error while downloading")
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
fmt.Println("Error while downloading")
}
func submit(c *gin.Context) {
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
url := c.Param("url")
fmt.Println("url " + url)
task := downloadFile(url)
hashFile(task.ID)
c.JSON(200, task.ID)
}
func main() {
router := gin.Default()
router.POST("/submit/:url", submit)
}
答案 0 :(得分:2)
HTTP状态404表示服务器找不到请求的URL。鉴于您的curl命令,这似乎很合理。您似乎在请求URL http://localhost:8000/submit
,但是您的应用程序只有一条路由:
router.POST("/submit/:url", submit)
此路由在/submit
之后需要第二个URL段,例如/submit/foo
。