Answer the question
In order to leave comments, you need to log in
How to use NewRequest in golang?
Hello!
There is such a code
package main
import (
"fmt"
// "net"
"net/http"
)
func main() {
client := &http.Client{}
fmt.Println("Hello, playground")
req, err := http.NewRequest("POST", "http://example.com", nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("If-None-Match", `W/"wyzzy"`)
req.Form.Add("username", "a")
req.Form.Add("password", "b")
resp, err := client.Do(req)
//k:=resp.Body
fmt.Printf("%+v\n", resp)
}
Answer the question
In order to leave comments, you need to log in
buffer := new(bytes.Buffer)
params := url.Values{}
params.Set("username", "a")
params.Set("password", "b")
buffer.WriteString(params.Encode())
req, _ := http.NewRequest("POST", "", buffer)
req.Header.Set("content-type", "application/x-www-form-urlencoded")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question