Closed uptutu closed 10 months ago
@uptutu Can you change this line from
_httpClient = resty.NewWithClient(&http.Client{Transport: http.DefaultTransport})
to
_httpClient = resty.New()
Try and share your feedback.
@uptutu Can you change this line from
_httpClient = resty.NewWithClient(&http.Client{Transport: http.DefaultTransport})
to
_httpClient = resty.New()
Try and share your feedback.
I use resty.New()
by default, but no matter what I change, the problem persists!
@uptutu I see then I will have to try your code snippet(s) locally. Let me give it a try!
@uptutu I have tried your code; it does work correctly for the default HTTP and Resty client. Regarding the connection reset by a peer, you should look into it at your end.
I have tried this on my end (I commented out the lines not present on my end and your code).
package main
import (
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/go-resty/resty/v2"
)
func main() {
url := "http://0.0.0.0:23360"
method := "POST"
payload := strings.NewReader(`Hi`)
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Connection", "close")
res, err := httpClient().GetClient().Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
time.Sleep(2 * time.Second)
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Response from server:", string(body))
// Resty Code
resp, err := httpClient().R().
SetHeader("Content-Type", "text/plain").
SetBody(strings.NewReader(`Hi`)).
SetContentLength(true).
Post(url)
fmt.Println("Resty code, response from server:", resp, err)
}
var _once sync.Once
var _httpClient *resty.Client
func httpClient() *resty.Client {
if _httpClient == nil {
_once.Do(func() {
_httpClient = resty.NewWithClient(&http.Client{Transport: http.DefaultTransport})
// _httpClient.SetTimeout(_defaultHTTPRequestTimeout)
// _httpClient.JSONMarshal = sonic.Marshal
// _httpClient.JSONUnmarshal = sonic.Unmarshal
_httpClient.SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
req.Close = true
return nil
})
})
}
return _httpClient
}
Output:
Response from server: {'code': '0', 'msg': ''}
Resty code, response from server: {'code': '0', 'msg': ''} read tcp 127.0.0.1:59384->127.0.0.1:23360: read: connection reset by peer
This's an awesome tool, Thank you to all the contributors for their efforts!
But I'm experiencing some confusion in using it
I have a TCP service for receiving client requests (I'll give the code below), I've tried using the
HTTP Native Client
, and thePostman
request receives a response from the server, but inresty
it gives an error:TCP Server Code
Resty Test Code
Native Go HTTP Client Test Code