Connection reset by peer GO - http

I'm trying to do a request in GO but I always receive "Connection reset by peer" error. The following code shows how I'm doing the request:
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
client := = &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fmt.Println(resp.Body)
...and I receive:
Get https://example.com: read tcp 1.2.3.4:1234->5.6.7.8:5678: read: connection reset by peer
When I do curl https://example.com I receive response form the server.
Why can't I do the request in GO?

Your code works if I use it against a URL like https://example.com. Are you sure you are passing it the correct URL?
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "https://example.com"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("OK")
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
str := string(bytes[:])
fmt.Printf("%s", str)
}

Related

How to reqeust using Http/2.0 in http library with DialTLS?

i want to request with http library in Golang using http2, i tried almost everything and made a lot of research without any success so i am asking here if any one can help me and give me the right answer.
package main
import (
"fmt"
"net"
"net/http"
"strings"
tls "github.com/refraction-networking/utls"
)
func main() {
req, _ := http.NewRequest("GET", "https://http2.pro/api/v1", nil)
tlsspec, _ := ParseJA3("771,49195-49196-52393-49199-49200-52392-49161-49162-49171-49172-156-157-47-53,65281-0-23-35-13-5-16-11-10,29-23-24,0")
transport := &http.Transport{
DialTLS: func(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
config := &tls.Config{ServerName: host}
uconn := tls.UClient(conn, config, tls.HelloCustom)
//tlsspec is just custom tls
if uconn.ApplyPreset(tlsspec); err != nil {
return nil, err
}
if uconn.Handshake(); err != nil {
return nil, err
}
return uconn, nil
},
}
client := http.Client{
Transport: transport,
}
resp, _ := client.Do(req)
fmt.Println(resp)
}

Troubles on constructing HLS using libp2p-http

I intend to implement a classic HLS example with libp2p, which is as follows
https://hackernoon.com/building-a-media-streaming-server-using-go-and-hls-protocol-j85h3wem
But I encountered the following errors:
404 page not found
My code constructing linstening:
m1, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1000")
m2, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1001")
srvHost := NewHost(m1)
clientHost := NewHost(m2)
defer srvHost.Close()
defer clientHost.Close()
srvHost.Peerstore().AddAddrs(clientHost.ID(), clientHost.Addrs(), peerstore.PermanentAddrTTL)
fmt.Println("id is", clientHost.ID())
fmt.Println("addr is", srvHost.Addrs())
clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL)
listener, err := gostream.Listen(srvHost, "/testiti-test")
if err != nil {
panic(err)
}
defer listener.Close()
My code constructing client:
tr := &http.Transport{}
tr.RegisterProtocol("libp2p", p2phttp.NewTransport(clientHost, p2phttp.ProtocolOption("/testiti-test")))
client := &http.Client{Transport: tr}
res, err := client.Get(fmt.Sprintf("libp2p://%s/simple", port))
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
My code constructing sever:
go func() {
http.HandleFunc("/simple", addHeaders(http.FileServer(http.Dir(songsDir))))
//http.HandleFunc("/simple", addHeaders2())
fmt.Printf("Starting server on %v\n", port)
log.Printf("Serving %s on HTTP port: %v\n", songsDir, port)
server := &http.Server{}
server.Serve(listener)
}()
func addHeaders(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
h.ServeHTTP(w, r)
}
}
New host:
func NewHost(listen multiaddr.Multiaddr) host.Host {
h, err := libp2p.New(
libp2p.ListenAddrs(listen),
)
if err != nil {
panic(err)
}
return h
}
The problem is casued by routing.For a http.FileServer,"\simple" means a dir but not a url.So just fix like this:
url := fmt.Sprintf("libp2p://%s/origin.m3u8", port)
res, err := client.Get(url)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))

How to send post form data through a http client?

I have a problem with post request, needed to send simple form data through http client.
http.PostForm() is not ok because I need to set my own user agent and other headers.
Here is the sample
func main() {
formData := url.Values{
"form1": {"value1"},
"form2": {"value2"},
}
client := &http.Client{}
//Not working, the post data is not a form
req, err := http.NewRequest("POST", "http://test.local/api.php", strings.NewReader(formData.Encode()))
if err != nil {
log.Fatalln(err)
}
req.Header.Set("User-Agent", "Golang_Super_Bot/0.1")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
You also need to set the content type to application/x-www-form-urlencoded which corresponds to the encoding used by Value.Encode().
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
This is mentioned as one of the things done by Client.PostForm.

Why the error occured when i tried to send a POST request?

I try to send the POST a request into my web server, but when i try to get response body occurs error. Also I tried to send request with Postman and everything worked fine. Response from server is JSON data which give some information about loaded picture.
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "localhost:6000/..."
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/home/...")
defer file.Close()
part1, errFile1 := writer.CreateFormFile("Image",filepath.Base("/home/..."))
_, errFile1 = io.Copy(part1, file)
if errFile1 !=nil {
fmt.Println(errFile1)
}
err := writer.Close()
if err != nil {
fmt.Println(err)
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
This is your line #42-43 where the error occurs:
res, err := client.Do(req)
defer res.Body.Close()
If client.Do() returns an error, res may be nil, and so res.Body.Close() is a runtime panic. You have to first check the error, and only proceed to close the body if error is nil:
res, err := client.Do(req)
if err != nil {
// Handle error and return:
return
}
defer res.Body.Close()
See related: Do we need to close the response object if an error occurs while calling http.Get(url)?
Also: Is resp.Body.Close() required if I don't need response?

How to make a long connection with http.Client?

I try to connect a http server as long connection, like below:
func main() {
request, err := http.NewRequest("GET", "http://long.connection.org:8080/", nil)
request.SetBasicAuth("xxx", "oooo")
http_client := &http.Client{}
response, _ := http_client.Do(request)
var buf []byte
for {
_, err := response.Body.Read(buf)
if err == io.EOF { break }
fmt.Printf("%s", string(buf))
}
}
But read from response.Body always empty. And seems I can't use response.Body to send data to server.
Any one can help?
This seems to work:
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func main() {
request, err := http.NewRequest("GET", "http://www.example.com/", nil)
if err != nil {
log.Fatal(err)
}
http_client := &http.Client{}
response, err := http_client.Do(request)
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 4096) // any non zero value will do, try '1'.
for {
n, err := response.Body.Read(buf)
if n == 0 && err != nil { // simplified
break
}
fmt.Printf("%s", buf[:n]) // no need to convert to string here
}
fmt.Println()
}
Edit: Added forgotten error handling of NewRequest.

Resources