Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jsonclient
- import (
- "bytes"
- "net"
- "time"
- "io/ioutil"
- "net/http"
- )
- type Config struct {
- Url string
- Data []byte
- Method string
- }
- type Client struct {
- Config *Config
- HttpClient *http.Client
- }
- func NewClient(c *Config) (*Client, error) {
- s := new(Client)
- // HTTP client
- s.Config = c
- s.HttpClient = &http.Client{
- Transport: &http.Transport{
- Dial: func(network, addr string) (net.Conn, error) {
- return net.DialTimeout(network, addr, time.Duration(time.Second*3))
- },
- },
- }
- return s, nil
- }
- func Dial(c *Client) string {
- req, err := http.NewRequest(c.Config.Method, c.Config.Url, bytes.NewBuffer(c.Config.Data))
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Accept", "application/json")
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- //fmt.Println("response Status:", resp.Status)
- //fmt.Println("response Headers:", resp.Header)
- body, _ := ioutil.ReadAll(resp.Body)
- return string(body)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement