1

As in many golang net/http articles, a request returns two values: response and error:

resp, err := http.Get("http://example.com/")
if err != nil {
    // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

For http-related errors, it will be returned in resp with status code like 502, 400 etc. What are the possible errors returned? I need to know them before I can handle them.

3
  • 1
    golang.org/pkg/net/http/#pkg-constants Commented Nov 29, 2016 at 3:38
  • @Marty I did not mean status code error, because they are returned in resp, I want to know that is returned as err in the first line of above code. Commented Nov 29, 2016 at 3:39
  • I was looking for this same question bur instead got an question which had 5 down votes and closed. Thanks @cizixs for asking this question. Commented Jun 22, 2018 at 12:23

3 Answers 3

7
  1. URL parse error
  2. too much redirect times
  3. tcp connect\write\read timeout
  4. 302 status, but null Location header

and so on

You can read the source code in http package. Then you can find all the errors returned by this function http.Get.

Sign up to request clarification or add additional context in comments.

Comments

0

Those errors will mostly network related errors, like network timeouts etc. There is no need to handle them differently. You may exit gracefully like,

resp, err := http.Get("http://google.com/")
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(1)
}

Comments

-1

there are many error responses like :-

301 (Moved Permanently)
302 (Found)
303 (See Other)
307 (Temporary Redirect) 

better you go and read http.Get

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.