How can I check specific golang net/http error code? - go

package main
import "net/http"
func main() {
req, err := http.NewRequest("GET", "http://domain_does_not_exist", nil)
if err != nil { panic("NewRequest") }
client := http.Client{ }
_, err = client.Do(req)
if err == ???
}
I would like to check my GET request for specific error(DNS resolve error). How to accomplish this?

Package "errors" has functions As, Is to unwrap specific error types, and package "net" has a *DNSError type. So:
var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
...
}

Related

Unable to list folders in GCP using go client library

I am going through the documentation of cloudresourcemanager package and trying to build a simple example to list the folders of my GCP project.
The following example however fails
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v2"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
fmt.Println(foldersService)
foldersListCall := foldersService.List()
resp, err := foldersListCall.Do()
if err != nil {
fmt.Println("Here")
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.Name)
}
}
It fails in
resp, err := foldersListCall.Do()
and the error is
googleapi: Error 400: Request contains an invalid argument., badRequest
I have the following environment variables set
GOOGLE_CLOUD_PROJECT=my-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json
and gcloud cli works fine.
Any suggestions what I might be missing?
The error message is not helpful at all...
The problem is I was not setting the Parent parameter in the request, i.e. the organization (switching to v3 helped a bit)
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
foldersListCall := foldersService.List()
foldersListCall.Parent("organizations/12345678910")
resp, err := foldersListCall.Do()
if err != nil {
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.DisplayName)
}
}

Semantic way of http.Response receiver functions in Go

I just started learning GO and wrote this piece of code that writes an http.Response.Body to os.Stdout or to a file, but I'm not happy about the semantics of this.
I want the http.Response struct to have these receiver functions, so I can use it more easily throughout the entire app.
I know that the answers might get flagged as opinionated, but I still wonder, is there a better way of writing this?
Is there some sort of best practice?
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
type httpResp http.Response
func main() {
res, err := http.Get("http://www.stackoverflow.com")
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
defer res.Body.Close()
response := httpResp(*res)
response.toFile("stckovrflw.html")
response.toStdOut()
}
func (r httpResp) toFile(filename string) {
str, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
ioutil.WriteFile(filename, []byte(str), 0666)
}
func (r httpResp) toStdOut() {
_, err := io.Copy(os.Stdout, r.Body)
if err != nil {
panic(err)
}
}
On a side note, is there a way to make the http.Get method spit out a custom type that already has access to these receiver functions without the need for casting? So i could do something like this:
func main() {
res, err := http.Get("http://www.stackoverflow.com")
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
defer res.Body.Close()
res.toFile("stckovrflw.html")
res.toStdOut()
}
Thanks!
You don't have to implement these functions. *http.Response already implements io.Writer:
Write writes r to w in the HTTP/1.x server response format, including the status line, headers, body, and optional trailer.
package main
import (
"net/http"
"os"
)
func main() {
r := &http.Response{}
r.Write(os.Stdout)
}
In the example above, the zero value prints:
HTTP/0.0 000 status code 0
Content-Length: 0
Playground: https://play.golang.org/p/2AUEAUPCA8j
In case you need additional business logic in the write methods, you can embed *http.Response in your defined type:
type RespWrapper struct {
*http.Response
}
func (w *RespWrapper) toStdOut() {
_, err := io.Copy(os.Stdout, w.Body)
if err != nil {
panic(err)
}
}
But then you must construct a variable of type RespWrapper with the *http.Response:
func main() {
// resp with a fake body
r := &http.Response{Body: io.NopCloser(strings.NewReader("foo"))}
// or r, _ := http.Get("example.com")
// construct the wrapper
wrapper := &RespWrapper{Response: r}
wrapper.toStdOut()
}
is there a way to make the http.Get method spit out a custom type
No, the return types of http.Get are (resp *http.Response, err error), that's part of the function signature, you can't change it.

Not getting response from HTTP Get Method

Implemented an logic in go to fetch the information from given URL,The problem is response of net/http is empty.
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("https://azure.microsoft.com/en-us/")
if err != nil {
// handle error
}
body, err := ioutil.ReadAll(resp.Body)
bodyString := string(body)
fmt.Print(bodyString)
fmt.Printf("%v %v", body, err)
}
Output: its returning empty slice instead of returning HTML content
[]byte{} <nil>
I'm using Go version 1.14.3.
It seems that's working when you set the User-Agent header :
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://azure.microsoft.com/en-us/", nil)
req.Header.Add("User-Agent", "Mozilla")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(resp.Body)
bodyString := string(body)
fmt.Print(bodyString)
}

Can't find a public file from url in go

I am trying to get the content of a publicly available file using ioutil.ReadFile() but it doesn't find the file: panic: open http://www.pdf995.com/samples/pdf.pdf: No such file or directory
Here's my code:
// Reading and writing files are basic tasks needed for
// many Go programs. First we'll look at some examples of
// reading files.
package main
import (
"fmt"
"io/ioutil"
)
// Reading files requires checking most calls for errors.
// This helper will streamline our error checks below.
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
fileInUrl, err := ioutil.ReadFile("http://www.pdf995.com/samples/pdf.pdf")
if err != nil {
panic(err)
}
fmt.Printf("HERE --- fileInUrl: %+v", fileInUrl)
}
Here's a go playground example
ioutil.ReadFile() does not support http.
If you look at the source code(https://golang.org/src/io/ioutil/ioutil.go?s=1503:1549#L42), open the file using os.Open.
I think I can do this coding.
package main
import (
"io"
"net/http"
"os"
)
func main() {
fileUrl := "http://www.pdf995.com/samples/pdf.pdf"
if err := DownloadFile("example.pdf", fileUrl); err != nil {
panic(err)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
but, go playgound not protocol(go error dial tcp: Protocol not available).
so, You have to do it PC.

Infinite redirect loop with Gorilla toolkit

I have this simple code:
import (
"log"
"github.com/gorilla/http"
"bytes"
)
func main() {
url := "https://www.telegram.org"
log.Println("url: " + url)
var b bytes.Buffer
http.Get(&b, url)
log.Println("Get done")
}
and it freezes on the line making the GET request. It seems that it enters an infinite loop of 302 responses which redirects to the same url ("https://www.telegram.org").
Am I doing or assuming something wrong?
Thanks and regards.
Apparently that library doesn't support https (lol)
https://github.com/gorilla/http/issues/8
So just use the stdlib http module:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
res, err := http.Get("https://www.telegram.org")
if err != nil {
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
fmt.Printf("%s", body)
}

Resources