Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[golang example] Use http.DefaultClient instead of your own client #5

Closed
DanielHeath opened this issue Mar 13, 2015 · 5 comments
Closed

Comments

@DanielHeath
Copy link

package main

import (
    "fmt"
    "strings"
    "net/http"
    "io/ioutil"
)

func main() {

    url := "http://mockbin.com/request?foo=bar&foo=baz"

    payload := strings.NewReader("{\"foo\": \"bar\"}")

    req, _ := http.NewRequest("POST", url, payload)

    req.Header.Add("cookie", "foo=bar; bar=baz")
    req.Header.Add("accept", "application/json")
    req.Header.Add("content-type", "application/json")

    res, _ := http.DefaultClient.Do(req)

    defer res.Body.Close()
    body, _ := ioutil.ReadAll(res.Body)

    fmt.Println(res)
    fmt.Println(string(body))

}
@montanaflynn
Copy link
Contributor

Is there a benefit to this approach besides one less line? The httpsnippet project is using the client for a timeout option and I'll probably add a redirect option as well.

@DanielHeath
Copy link
Author

Nothing wrong with it, it's just not idiomatic go (unless you use the client to do something).

@montanaflynn
Copy link
Contributor

Thanks for the input. Like I said the underlying library has a timeout option so that's why it's there, I might make it as you suggest for when the option is not invoked or maybe just set a default timeout.

@ahmadnassri
Copy link
Contributor

@Montana you could also make two separate implementations and let the user decide which they'd like to use.

also, this would be better suited as a discussion on https://github.com/Mashape/httpsnippet

@montanaflynn
Copy link
Contributor

Closed with Kong/httpsnippet@45a0e36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants