Giter Club home page Giter Club logo

Comments (4)

jeevatkm avatar jeevatkm commented on May 28, 2024

@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 avatar uptutu commented on May 28, 2024

@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.

jeevatkm avatar jeevatkm commented on May 28, 2024

@uptutu I see then I will have to try your code snippet(s) locally. Let me give it a try!

from resty.

jeevatkm avatar jeevatkm commented on May 28, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.