douzhanlai4671 2018-12-16 13:39
浏览 675
已采纳

Go中的'net / http'中的request.FormValue为空

I'm trying to create a simple http service with the endpoint to download file to the local system in Go. The link comes in ?uri tag, but when I want to get it I receive an empty string. I tried to parse the form of my request but it didn't help. Here is my code:

func main() {
http.HandleFunc("/download", DownloadHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func DownloadHandler(writer http.ResponseWriter, request *http.Request) {
    prsErr := request.ParseForm()
    if prsErr != nil{
        panic(prsErr)
    }
    uri := request.FormValue("?uri")
    _, _ = writer.Write([]byte(uri))
    err := DownloadFile("img.png", uri)
    if err != nil {
        panic(err)
    }
}

func DownloadFile(filepath string, url string) error {

    // Create the file
    out, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer out.Close()

    // Get the data
    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    if err != nil {
        return err
    }

    return nil
}

I will appreciate any help! Thank you!

  • 写回答

1条回答 默认 最新

  • dsgw8802 2018-12-16 13:43
    关注

    invalid at request.FormValue("?uri")

    uri := request.FormValue("uri")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料