Created
September 26, 2019 17:57
-
-
Save Youngestdev/b183be1a4b1c5284d2fe007a8d66ebe8 to your computer and use it in GitHub Desktop.
Learning about the http lib from next.tech
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| "net/url" | |
| "strings" | |
| ) | |
| type StringServer string | |
| func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | |
| req.ParseForm() | |
| fmt.Printf("Received form data: %v\n", req.Form) | |
| fmt.Printf("Received header: %v\n", req.Header) | |
| rw.Write([]byte(string(s))) | |
| } | |
| func createServer(addr string) http.Server { | |
| return http.Server{ | |
| Addr: addr, | |
| Handler: StringServer("Hello world"), | |
| } | |
| } | |
| const addr = "localhost:7070" | |
| func main() { | |
| s := createServer(addr) | |
| go s.ListenAndServe() | |
| form := url.Values{} | |
| form.Set("id", "5") | |
| form.Set("name", "Wolfgang") | |
| req, err := http.NewRequest(http.MethodPost, | |
| "http://localhost:7070", | |
| strings.NewReader(form.Encode())) | |
| if err != nil { | |
| panic(err) | |
| } | |
| req.Header.Set("Content-Type", | |
| "application/x-www-form-urlencoded") | |
| res, err := http.DefaultClient.Do(req) | |
| if err != nil { | |
| panic(err) | |
| } | |
| data, err := ioutil.ReadAll(res.Body) | |
| if err != nil { | |
| panic(err) | |
| } | |
| res.Body.Close() | |
| fmt.Println("Response from server:" + string(data)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment