3

Example:

package main

import (
    "fmt"
    "log"
    "net/url"
)

func main() {
    u, err := url.Parse("http://bing.com/search?q=dotnet")
    if err != nil {
        log.Fatal(err)
    }
    u.Scheme = "https"
    u.Host = "google.com"
    q := u.Query()
    q.Set("q", "golang../")
    u.RawQuery = q.Encode()
    fmt.Println(u)
}

Output: https://google.com/search?q=golang..%2F here "/" is encoded to "%2F" what to do if we don't what this and want something like https://google.com/search?q=golang../ I tried to find a lot on google but didn't get anything

2
  • 1
    Why would you want to assemble an invalid URL? Commented Sep 13, 2020 at 17:03
  • %2F didn't work as intended if manually put "/" it works as intended Commented Sep 13, 2020 at 17:07

2 Answers 2

5

So I Found an Answer to this here the code

package main

import (
    "fmt"
    "log"
    "net/url"
)

func main() {
    u, err := url.Parse("http://bing.com/search?q=dotnet")
    if err != nil {
        log.Fatal(err)
    }
    u.Scheme = "https"
    u.Host = "google.com"
    q := u.Query()
    q.Set("q", "golang../")
    u.RawQuery = q.Encode()
    decoded,err:=url.QueryUnescape(q.Encode())
    u.RawQuery= decoded
    if err != nil {
        return
    } 
    fmt.Println(u)
}

had to use url.QueryUnescape

Sign up to request clarification or add additional context in comments.

Comments

3

Set the raw query to what you need, not to the encoded value:

u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
   log.Fatal(err)
}
u.Scheme = "https"
u.Host = "google.com"
u.RawQuery = fmt.Sprintf("q=golang../")
fmt.Println(u)

1 Comment

the above code is just an example for the same code I used in my main code and I have undefined params but defined payloads which I need to send as a value in those query params. so this solution is not applicable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.