Comments (4)
@uptutu Can you change this line
from
_httpClient = resty.NewWithClient(&http.Client{Transport: http.DefaultTransport})
to
_httpClient = resty.New()
Try and share your feedback.
from resty.
@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!
from resty.
@uptutu I see then I will have to try your code snippet(s) locally. Let me give it a try!
from resty.
@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
from resty.
Related Issues (20)
- Breaks application from Ctrl-C handling HOT 1
- Brotli support HOT 1
- How to handle response if a call to API returns 200 with struct A, or 201 with struct B? HOT 3
- SetResult failed with httpmock HOT 2
- CVE-2023-45286: HTTP request body disclosure HOT 3
- SetProxy HOT 3
- The request with SetFileReader retry time great than 1 will has issues HOT 1
- Signing requests with path parameters HOT 1
- Clone or copy of existing client HOT 6
- cache middleware HOT 5
- retrying of non-idempotent methods HOT 2
- Path param encoding is broken
- can't use image.decode on rawbody HOT 1
- Can it support socket5 proxy? HOT 2
- Resty retry function does not work in Slack as custom http client HOT 2
- Middleware around resty.Request.Execute including retries HOT 1
- about proxy
- Client: Improve its creation flow and its options
- Request: Improve Request struct and its usage
- Support query parameters without encoding HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resty.