I won't say much nonsense, let's just read the code~
package main import ( "net/http" "net/url" "fmt" "io/ioutil" _ "io" "bytes" ) func main() { postFile() } func post() { //This is the address that a Post parameter will be returned strinUrl:="http://localhost:8080/aaa"`Write code fragment here` resopne,err:= (strinUrl,{"num":{"456"},"num1":{"123"}}) if err !=nil { ("err=",err) } defer func() { () ("finish") }() body,err:=() if err!=nil { (" post err=",err) } (string(body)) } func postFile(){ //This is the address that a Post parameter will be returned strinUrl:="http://localhost:8080/aaa" byte,err:=("") resopne,err :=(strinUrl,"multipart/form-data",(byte)) if err !=nil { ("err=",err) } defer func() { () ("finish") }() body,err:=() if err!=nil { (" post err=",err) } (string(body)) }
Water drops wear stones. Here we also paste the function of Go Http Post parameter to process the main comparison of the differences between the two.
Supplement: golang crawler simulates post requests for various situations File upload
Go implements various types of post requests
Request a test address
var ( requestPostURL string = "/post" // The service that receives files is implemented by itself qwq // Receive an image upload postman's key file imagePostURL string = "/imageUpload/upload" // Receive multiple pictures to upload postman's key file imageMultiPostURL string = "/imageUpload/uploads" )
application/x-www-from-urlencoded
illustrate
application/x-www-from-urlencoded, converts the data in the form into key-value pairs, for example, name=java&age = 23
Example
// func postXWwwFromURLEncoded() { client := {} // Post without any request data // req, err := (, requestPostURL, nil) // With data urlValues := {} ("name", "Zhang San") ("age", "18") reqBody := () req, err := (, requestPostURL, (reqBody)) if err != nil { ("err") } resp, err := (req) if err != nil { ("err") } defer () b, err := () if err != nil { ("err") } (string(b)) // urlValues := {} // ("name","zhaofan") // ("age","22") // resp, _ := ("/post",urlValues) }
raw
illustrate
// That is, the parameter entry method is json
// You can upload text in any format, you can upload text, json, xml, and html
Example
func postRaw() { client := {} // with data json type urlValues := map[string]interface{}{ "name": "jack", "age": 18, "is_active": true, } b1, _ := (&urlValues) // b1, _ := (&urlValues) req, err := (, requestPostURL, (b1)) if err != nil { ("err") } resp, err := (req) if err != nil { ("err") } defer () b, err := () if err != nil { ("err") } (string(b)) }
multipart/form-data with normal parameters
illustrate
// multipart/form-data
// You can upload files or key-value pairs
// When the uploaded field is a file, Content-Type will be used to indicate the file type; content-disposition
// Multiple files can be uploaded
Example
// multipart/form-data with normal parameters key-valuefunc postFormDataWithParams() { client := {} // Post without any request data body := &{} writer := (body) params := map[string]string{ "name": "zhangsan", "age": "12", } for key, val := range params { _ = (key, val) } () req, err := (, requestPostURL, body) if err != nil { ("err") } resp, err := (req) if err != nil { ("err") } defer () b, err := () if err != nil { ("err") } (string(b)) }
multipart/form-data Upload a file
// key:file put a file// multipart/form-data to pass a filefunc postFormDataWithSingleFile() { client := {} bodyBuf := &{} bodyWrite := (bodyBuf) file, err := ("./images/") defer () if err != nil { ("err") } // file is key fileWrite, err := ("file", "") _, err = (fileWrite, file) if err != nil { ("err") } () //To close, the flash will be written to // Create a request contentType := () req, err := (, imagePostURL, bodyBuf) if err != nil { ("err") } // Set the header ("Content-Type", contentType) resp, err := (req) if err != nil { ("err") } defer () b, err := () if err != nil { ("err") } (string(b)) }
multipart/form-data Upload multiple files
// key:file put multiple files in it// multipart/form-data to transfer multiple filesfunc postFormDataWithMultipartFile() { client := {} bodyBuf := &{} bodyWrite := (bodyBuf) images := []string{"", ""} for _, val := range images { file, err := ("./images/" + val) defer () if err != nil { ("err") } fileWrite, err := ("file", val) _, err = (fileWrite, file) if err != nil { ("err") } } () //To close, the flash will be written to // Create a request req, err := (, imagePostURL, bodyBuf) if err != nil { ("err") } // Set the header contentType := () ("Content-Type", contentType) resp, err := (req) if err != nil { ("err") } defer () b, err := () if err != nil { ("err") } (string(b)) }
binary
// Content-Type:application/octet-stream, from the literal meaning, only binary data can be uploaded, usually used to upload files,// Since there are no key values, only one file can be uploaded at a timefunc postBinary() { }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.