golang http发送文件

    w := multipart.NewWriter(body)

    content_type := w.FormDataContentType()
    w.WriteField("api_id", Appid)
    w.WriteField("api_secret", Key)
    w.WriteField("name", r.Name)
    w.WriteField("id_number", r.IdCode)
    dd, err := base64.StdEncoding.DecodeString(r.Photo)
    fmt.Println(len(dd))
    fmt.Println(err)
    file, err := w.CreateFormFile("selfie_file", "selfie_file")
    file.Write(dd)
    w.Close()

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}

    request, _ := http.NewRequest("POST", RemoteUrl, body)
    request.Header.Set("Content-Type", content_type)

    rep, err := client.Do(request)
    if err != nil {
        log.Logger.Error(err.Error())
    }
    defer rep.Body.Close()

    bytes, _ := ioutil.ReadAll(rep.Body)```

你可能感兴趣的:(golang http发送文件)