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)
- 读写csv文件应当考虑可以自定义分隔符 HOT 2
- Feature Request: Read Large Files by Line and Support Seek HOT 1
- AesEcbDecrypt 以及slice的删除有问题: HOT 2
- 多线程安全 HOT 1
- stream Map Existing problem HOT 2
- maputil.MapTo在定义的struct中有指针时报panic: reflect: call of reflect.Value.Interface on zero Value HOT 2
- Documentation and Go playground seem broken HOT 2
- OS: windows, ARCH: 386 fail to build HOT 3
- strutil中是否可以增加SubBetween方法? HOT 4
- strutil.After() and strutil.Before() bug: length of source string less length of split char HOT 2
- Maputil.MapTo方法有误 HOT 2
- package /datastructure/set 'FromSlice' and 'Values' make me confuse HOT 1
- 受1brc项目影响,我认为可以来一个并发读取超大文本文件的工具函数 HOT 3
- Which branch should I submit to? HOT 3
- 四舍五入保留两位小数金额不对,64.585结果应该是64.59,但输出结果是64.58 HOT 4
- Maximum capacity overflows int HOT 1
- netutil.StructToUrlValues 当前不支持结构体嵌套转换 HOT 3
- 邮箱地址验证有误 HOT 2
- maputil.MapToStruct json标签中的omitempty会干扰转化 HOT 1
- 建议支持只导入一个包名,就可以使用所有函数的方式 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.