Last active
September 26, 2018 10:08
-
-
Save takayukioda/1fe90fab5d13ffd93a481cf876baa3c1 to your computer and use it in GitHub Desktop.
HTTP request with golang using "net/http" package
This file contains 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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/http/cookiejar" | |
"net/url" | |
"strings" | |
"time" | |
"golang.org/x/net/publicsuffix" | |
) | |
func main() { | |
client, err := newClient() | |
if err != nil { | |
log.Fatalln("Fail to make http.Client. Most likely failed on making cookie jar", err) | |
} | |
req, err := newRequest("GET", "https://example.com", url.Values{}) | |
if err != nil { | |
log.Fatalln("Fail to make http.Request", err) | |
} | |
buf := new(bytes.Buffer) | |
resp, err := do(client, req, buf) | |
if err != nil { | |
log.Fatal("Fail on HTTP request", err) | |
} | |
fmt.Println("status:", resp.StatusCode) | |
fmt.Println("body:", buf.Bytes()) | |
} | |
func newClient() (*http.Client, error) { | |
jar, err := cookiejar.New(&cookiejar.Options{ | |
PublicSuffixList: publicsuffix.List, | |
}) | |
if err != nil { | |
return nil, err | |
} | |
client := &http.Client{ | |
Jar: jar, | |
Timeout: 30 * time.Second, | |
} | |
return client, nil | |
} | |
func newRequest(method, path string, values url.Values) (*http.Request, error) { | |
body := strings.NewReader(values.Encode()) | |
req, err := http.NewRequest(method, path, body) | |
if err != nil { | |
return nil, err | |
} | |
return req, nil | |
} | |
func do(client *http.Client, req *http.Request, v interface{}) (*http.Response, error) { | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
if v != nil { | |
if w, ok := v.(io.Writer); ok { | |
// give *bytes.Buffer to get raw bytes instead of json decoded string | |
io.Copy(w, resp.Body) | |
} else { | |
err = json.NewDecoder(resp.Body).Decode(v) | |
// ignore the error caused by an empty response | |
if err == io.EOF { | |
err = nil | |
} | |
} | |
} | |
return resp, nil | |
} |
This file contains 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" | |
"io/ioutil" | |
"net/http" | |
"time" | |
) | |
func main() { | |
resp, err := request("GET", "https://example.com", nil) | |
if err != nil { | |
// handle error | |
} | |
fmt.Println(resp) | |
} | |
func request(method, path string, body io.Reader) (*http.Response, error) { | |
client := &http.Client{ | |
Timeout: 30 * time.Second, | |
} | |
req, err := http.NewRequest(method, path, body) | |
if err != nil { | |
return nil, err | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
data, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return nil, err | |
} | |
fmt.Println(data) | |
return resp, nil | |
} |
This file contains 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" | |
) | |
// https://golang.org/pkg/net/http/#pkg-overview | |
// simple GET request; dump all response to stdout | |
func main() { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", "https://example.com", nil) | |
if err != nil { | |
// handle error | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
// handle error | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
// handle error | |
} | |
fmt.Println(body) | |
} |
This file contains 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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io" | |
"net/http" | |
"time" | |
) | |
func main() { | |
buf := new(bytes.Buffer) | |
resp, err := request("GET", "https://example.com", nil, buf) | |
if err != nil { | |
// handle error | |
} | |
fmt.Println(resp) | |
fmt.Println(buf.Bytes()) | |
} | |
func request(method, path string, body io.Reader, v interface{}) (*http.Response, error) { | |
client := &http.Client{ | |
Timeout: 30 * time.Second, | |
} | |
req, err := http.NewRequest(method, path, body) | |
if err != nil { | |
return nil, err | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
if v != nil { | |
if w, ok := v.(io.Writer); ok { | |
// give *bytes.Buffer to get raw bytes instead of json decoded string | |
io.Copy(w, resp.Body) | |
} else { | |
err = json.NewDecoder(resp.Body).Decode(v) | |
// ignore the error caused by an empty response | |
if err == io.EOF { | |
err = nil | |
} | |
} | |
} | |
return resp, nil | |
} |
This file contains 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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io" | |
"net/http" | |
"net/http/cookiejar" | |
"net/url" | |
"strings" | |
"time" | |
"golang.org/x/net/publicsuffix" | |
) | |
func main() { | |
client, err := newClient() | |
if err != nil { | |
// handle error | |
} | |
req, err := newRequest("GET", "https://example.com", url.Values{}) | |
if err != nil { | |
// handle error | |
} | |
buf := new(bytes.Buffer) | |
resp, err := do(client, req, buf) | |
if err != nil { | |
// handle error | |
} | |
fmt.Println(resp) | |
fmt.Println(buf.Bytes()) | |
} | |
func newClient() (*http.Client, error) { | |
jar, err := cookiejar.New(&cookiejar.Options{ | |
PublicSuffixList: publicsuffix.List, | |
}) | |
if err != nil { | |
return nil, err | |
} | |
client := &http.Client{ | |
Jar: jar, | |
Timeout: 30 * time.Second, | |
} | |
return client, nil | |
} | |
func newRequest(method, path string, values url.Values) (*http.Request, error) { | |
body := strings.NewReader(values.Encode()) | |
req, err := http.NewRequest(method, path, body) | |
if err != nil { | |
return nil, err | |
} | |
return req, nil | |
} | |
func do(client *http.Client, req *http.Request, v interface{}) (*http.Response, error) { | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
if v != nil { | |
if w, ok := v.(io.Writer); ok { | |
// give *bytes.Buffer to get raw bytes instead of json decoded string | |
io.Copy(w, resp.Body) | |
} else { | |
err = json.NewDecoder(resp.Body).Decode(v) | |
// ignore the error caused by an empty response | |
if err == io.EOF { | |
err = nil | |
} | |
} | |
} | |
return resp, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment