Home > Backend Development > Golang > Error while making HTTP GET request in Go: 'dial tcp:lookup api.ipify.org on:53:read udp:50975->:53:read:connectionrejected'

Error while making HTTP GET request in Go: 'dial tcp:lookup api.ipify.org on:53:read udp:50975->:53:read:connectionrejected'

王林
Release: 2024-02-10 11:57:21
forward
1354 people have browsed it

在 Go 中发出 HTTP GET 请求时出错:“dial tcp:lookup api.ipify.org on:53:read udp:50975->:53:read:connectionrejected”

In the Go language, when we try to send an HTTP GET request, sometimes we encounter a message called “dial tcp: lookup api.ipify. org on: 53: read udp: 50975->: 53: read: connection rejected" error. This error usually appears in terms of network connections and it means that our program cannot establish a connection to the target server. There may be many reasons for this error, such as network failure, DNS resolution issues, or firewall restrictions. The solution to this problem depends on the specific situation. We can try to check the network settings, verify whether the DNS resolution is correct, eliminate firewall restrictions, etc. By resolving this error, we can ensure that our Go program can properly send HTTP requests and obtain the required data.

Question content

I am trying to make an HTTP GET request in Go using the net/http package to retrieve my public IP address using the api.ipify.org service . I get the error only after building the application using:

CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -o ip get.go
Copy after login

However, I noticed that the code runs perfectly fine when I use the following command:

go build get.go
Copy after login
go run get.go
Copy after login

This is the code I'm using:

package main

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

func get() {
    resp, err := http.Get("https://api.ipify.org")
    if err == nil {
        defer resp.Body.Close()
        bodyBytes, _ := ioutil.ReadAll(resp.Body)
        body := string(bodyBytes)
        fmt.Println(body)
    } else {
        fmt.Println(err)
    }
}

func main() {
    get()
}
Copy after login

What confuses me is why the code works with go build and go run but fails when I use more complex build commands. Can anyone help me understand what might be causing this problem and how to fix it? Thanks for your time and help!

Workaround

When cgo is available, the cgo-based parser will be used. It uses various means to resolve domains that are reading /etc/resolv.conf or /etc/nsswitch.conf etc. These features are not implemented by the Go-based DNS resolver, resulting in domains that cannot be resolved and HTTP requests that fail.

The above is the detailed content of Error while making HTTP GET request in Go: 'dial tcp:lookup api.ipify.org on:53:read udp:50975->:53:read:connectionrejected'. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template