dpgu5074 2019-06-05 12:09
浏览 301

我尝试使用POST将文件下载到我的api时出现404错误吗?

Here api to go which should load the file when posting a request of the form

curl -X POST -d "url = http: //site.com/file.txt" http: // localhost: 8000 / submit

But 404 gets out, what's the reason? Or how to download files via POST in 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)
}
  • 写回答

1条回答 默认 最新

  • doucheng7534 2019-06-05 12:25
    关注

    HTTP status 404 means the server couldn't find the requested URL. This appears to make perfect sense given your curl command. You appear to be requesting the URL http://localhost:8000/submit, but your application only has a single route:

    router.POST("/submit/:url", submit)
    

    This route requires a second URL segment after /submit, such as /submit/foo.

    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM