resty icon indicating copy to clipboard operation
resty copied to clipboard

SetQueryParams failed in onBeforeRequest

Open jwrookie opened this issue 4 years ago • 2 comments

succeed

r := client.R()
if rsp, err = r.SetBody(body).SetQueryParams(map[string]string{
    "ts":          xxx,
    "fCode":   xxx,
    "version": xxx,
}).Post("http://xxx"); err != nil {
return err
}

failed

client := resty.New().OnError(onError)
r := client.R()
if rsp, err = r.SetBody(body).Post("http://xxx"); err != nil {
    return err
}

func onBeforeRequest(c *resty.Client, r *resty.Request) error{
        r.SetQueryParams(map[string]string{
		"ts":      xxx,
		"fCode":   xxx,
		"version": xxx,
	})
}

jwrookie avatar Mar 22 '21 12:03 jwrookie

Is this a typo? Where are you using onBeforeRequest?

moorereason avatar Mar 22 '21 18:03 moorereason

client := resty.New().OnBeforeRequest(onBeforeRequest)
client.SetProxy("http://127.0.0.1:8888")
client.SetRetryCount(2)
r := client.R()
if rsp, err = r.SetBody(body).Post("httxp://xx"); err != nil {
    return err
}

func onBeforeRequest(c *resty.Client, r *resty.Request) error{
        r.SetQueryParams(map[string]string{
		"ts":      xxx,
		"fCode":   xxx,
		"version": xxx,
	})
}

2021/03/23 11:17:52.135393 ERROR RESTY Post "https://webgfw.ymt.com/hangqing_ditu/api/hot_market_price_list": proxyconnect tcp: dial tcp 127.0.0.1:8888: connect: connection refused, Attempt 1

2021/03/23 11:17:52.237422 ERROR RESTY Post "https://webgfw.ymt.com/hangqing_ditu/api/hot_market_price_list?fCode=xxx&ts=xxx&version=xxx": proxyconnect tcp: dial tcp 127.0.0.1:8888: connect: connection refused, Attempt 2

2021/03/23 11:17:52.388582 ERROR RESTY Post "https://webgfw.ymt.com/hangqing_ditu/api/hot_market_price_list?fCode=xxx&ts=xxx&version=xxx": proxyconnect tcp: dial tcp 127.0.0.1:8888: connect: connection refused, Attempt 3

jwrookie avatar Mar 23 '21 03:03 jwrookie

@jwrookie I'm unable to reproduce the issue. I just wanted to let you know that your provided code snippet has a problem/error. This is what I did -

package main

import (
	"fmt"

	"github.com/go-resty/resty/v2"
)

func main() {
	onBeforeRequest := func(c *resty.Client, r *resty.Request) error {
		r.SetQueryParams(map[string]string{
			"ts":      "xxx",
			"fCode":   "xxx",
			"version": "1.0.111",
		})

		return nil
	}

	client := resty.New().OnBeforeRequest(onBeforeRequest)
	client.SetProxy("http://15.204.161.192:18080") // grabbed from https://www.sslproxies.org/
	client.SetRetryCount(2)
	r := client.R()
	rsp, err := r.SetBody("This is test payload").Post("https://httpbin.org/post")
	if err != nil {
		fmt.Println(err)

	}
	fmt.Println(rsp)
}

Output:

{
  "args": {
    "fCode": "xxx", 
    "ts": "xxx", 
    "version": "1.0.111"
  }, 
  "data": "This is test payload", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "20", 
    "Content-Type": "text/plain; charset=utf-8", 
    "Host": "httpbin.org", 
    "User-Agent": "go-resty/2.9.1 (https://github.com/go-resty/resty)", 
    "X-Amzn-Trace-Id": "Root=1-6519e550-2f7ac80e4a7f955640f08544"
  }, 
  "json": null, 
  "origin": "77.111.247.76", 
  "url": "https://httpbin.org/post?fCode=xxx&ts=xxx&version=1.0.111"
}

jeevatkm avatar Oct 01 '23 21:10 jeevatkm