Comments (1)
multipart/form-data 中有 boundary,该怎么写?
@Bisns 使用writer.FormDataContentType 设置Content-Type
header 为 multipart/form-data; boundary=xxx
。参考代码:
- server.go
package main
import (
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/upload", handleUploadFile)
http.ListenAndServe(":8090", nil)
}
func handleUploadFile(w http.ResponseWriter, r *http.Request) {
log.Println("Content-Type: ", r.Header.Get("Content-Type"))
if err := r.ParseMultipartForm(0); err != nil {
log.Fatal(err)
return
}
log.Println("FormValue(username): ", r.FormValue("username"))
log.Println("FormValue(password): ", r.FormValue("password"))
log.Println("PostFormValue(username):", r.PostFormValue("username"))
log.Println("PostFormValue(password): ", r.PostFormValue("password"))
// log.Println("MultipartForm: ", r.MultipartForm)
// file, header, err := r.FormFile("file")
// log.Println("FormFile(file): ", file, header, err)
w.Header().Set("Content-Type", "text/json")
io.WriteString(w, "ok")
}
- client.go
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
UploadFile()
}
func UploadFile() {
file, _ := os.Open("./test.txt")
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
if err != nil {
log.Fatal(err)
}
io.Copy(part, file)
writer.WriteField("username", "test")
writer.WriteField("password", "123456")
err = writer.Close()
if err != nil {
log.Fatal(err)
}
header := http.Header{}
header.Add("Content-Type", writer.FormDataContentType())
req := &netutil.HttpRequest{
RawURL: "http://127.0.0.1:8090/upload",
Method: "POST",
Headers: header,
Body: body.Bytes(),
}
client := netutil.NewHttpClient()
resp, err := client.SendRequest(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
s, _ := io.ReadAll(resp.Body)
fmt.Println("response: ", string(s)) //ok
}
from lancet.
Related Issues (20)
- There is an ambiguity in the use of the method `fileutil.CreateDir` HOT 5
- cryptor.HmacSha256 uses hex encoding by default, base64 encoding should be added HOT 1
- 如何使用datetime包获得指定时区的时间呢 HOT 2
- Windows下使用fileutil.zip 打包出来的内容不正确 HOT 6
- IsWeekend 函数可以考虑移除 HOT 2
- 能否增加下SM国密系列包 HOT 1
- GenerateRsaKey 能否直接输出[]Byte HOT 7
- slice.Unique能否增加predicate参数 HOT 1
- 建议AesCbcEncrypt支持自定义iv向量 HOT 1
- good project ,but get public ip is error HOT 5
- 设置超时时间没有效果 HOT 1
- 有时候两个结构很相似,只是扩展了部分字段 HOT 1
- 队列能自动扩容嘛 HOT 2
- sendRequest建议支持context HOT 2
- Promise内存布局优化,以及内存固定bug HOT 1
- [datastructure]使用 CopyOnWriteList 在测试中出现修改元素问题
- fileutil.MiMeType 不是很准确 HOT 1
- param precision of mathutil.Cos isn't used HOT 2
- 读写csv文件应当考虑可以自定义分隔符 HOT 2
- Feature Request: Read Large Files by Line and Support Seek HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lancet.