resty icon indicating copy to clipboard operation
resty copied to clipboard

Request URL as empty string

Open davidbayo10 opened this issue 3 years ago • 2 comments

Hi,

package main

import (
	"fmt"

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

func main() {
	client := resty.New()

	req := client.
		SetBaseURL("https://example.com").
		R().
		SetQueryParam("page", "1")


	/*
	result: ""
	expected: "https://example.com?page=1"
	*/
	fmt.Println(req.URL)

	req.Get("")

	req.SetQueryParam("page", "2")

	/*
	result: "https://example.com?page=1"
	expected: "https://example.com?page=2"
	*/
	fmt.Println(req.URL)
}

This piece reveals a weird behavior. Those prints are not correct. I am doing something wrong?. Thank you all in advance.

davidbayo10 avatar Nov 16 '21 14:11 davidbayo10

I'm pretty sure Request.URL is purposely not set until a request is finally executed. In your example, that happens with req.Get.

moorereason avatar Nov 16 '21 14:11 moorereason

So, the only way for caching requests is prebuilding the url. Right??

davidbayo10 avatar Nov 17 '21 08:11 davidbayo10