URL encoding, often known as percent-encoding, is a technique of encoding URLs utilizing solely a restricted set of characters in order that the URL could be transmitted safely. A typical URL consists of the next parts:
scheme://host:port/path?question
Within the examples under, we are going to present tips on how to encode the question a part of the URL, the path half, and likewise tips on how to construct a full encoded URL.
Encode the question a part of the URL
The url.QueryEscape()
operate from the internet/url
bundle is used to encode a string positioned inside a URL question.
bundle fundamental
import (
"fmt"
"internet/url"
)
func fundamental() {
query1Val := url.QueryEscape("ab+c")
query2Val := url.QueryEscape("de$f")
fmt.Println(query1Val)
fmt.Println(query2Val)
}
Output:
To create an encoded a number of key-value question parameters string, use the url.Values
construction from the internet/url
bundle.
bundle fundamental
import (
"fmt"
"internet/url"
)
func fundamental() {
queryValues := url.Values{}
queryValues.Add("question", "ab+c")
queryValues.Add("query2", "de$f")
encodedQuery := queryValues.Encode()
fmt.Println(encodedQuery)
}
Output:
question=abpercent2Bc&query2=depercent24f
Encode the trail a part of the URL
The url.PathEscape()
operate from the internet/url
bundle is used to encode a string that’s positioned inside a URL path section. The path section is encoded in a different way from the question, for instance, the +
character is allowed within the path, and ought to be encoded within the question.
bundle fundamental
import (
"fmt"
"internet/url"
)
func fundamental() {
path := url.PathEscape("foo+bar!")
fmt.Println(path)
}
Output:
Construct a full encoded URL
There are two methods to assemble a full encoded URL. You may create it manually by becoming a member of totally different elements of the URL, the place the question and path elements are escaped with the beforehand used features.
bundle fundamental
import (
"fmt"
"internet/url"
)
func fundamental() {
// construct url manually
host := "https://instance.com/"
path := url.PathEscape("foo+bar!")
query1Val := url.QueryEscape("ab+c")
query2Val := url.QueryEscape("de$f")
question := fmt.Sprintf("question=%s&query2=%s", query1Val, query2Val)
fmt.Printf("%s %spercents?%sn", "Manually constructed URL:", host, path, question)
}
Output:
Manually constructed URL: https://instance.com/foo+barpercent21?question=abpercent2Bc&query2=depercent24f
Nevertheless, it’s typically higher concept to construct the encoded URL by utilizing the url.URL
construction. This manner is less complicated and fewer error-prone than manually setting up the ensuing URL. You simply have to set the Scheme
, Host
, Path
of the URL and construct the RawQuery
string by encoding question parameters contained in the url.Values
struct.
bundle fundamental
import (
"fmt"
"internet/url"
)
func fundamental() {
// construct url utilizing url.URL struct
exampleURL := &url.URL{
Scheme: "https",
Host: "instance.com",
Path: "/foo+bar!",
}
queryValues := url.Values{}
queryValues.Add("question", "ab+c")
queryValues.Add("query2", "de$f")
exampleURL.RawQuery = queryValues.Encode()
fmt.Printf("%s %sn", "URL constructed utilizing url.URL struct:", exampleURL)
}
URL constructed utilizing url.URL struct: https://instance.com/foo+barpercent21?question=abpercent2Bc&query2=depercent24f