Skip to content

Go library to make handling http URLs easier

License

Notifications You must be signed in to change notification settings

square/httpurl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpurl

license travis coverage report

Minimalistic Go library to make handling http URLs easier.

httpurl complements Golang's url.URL from the net/url standard library. The desire to implement httpurl stems from:

  • noticing developers are using string manipulation functions to manipulate URLs/URIs. Inevitably, we end up with bits and pieces of incorrect code (especially when mixing regular expressions and URLs for domain validation).
  • the lack of url.MustParse.
  • query manipulation requiring going back and forth from url.Query to url.RawQuery.
  • somewhat inspired by Java's OkHttpUrl library. An early attempt to use a builder pattern didn't make production code any shorter or easier to write.

Documentation

https://pkg.go.dev/github.com/alokmenghrajani/httpurl

Example

Instead of:

u, err := url.Parse("http://example.com/")
if err != nil {
    // Need to handle this error
}
q := u.Query()
q.Add("code", "1234")
u.RawQuery = q.Encode()

You can do:

u := httpurl.MustParse("http://example.com")
AddQueryParam(u, "code", 1234)

* * *

And instead of :

basePath := "https://example.com/users/%d/products/%s/"

baseURL, err := url.Parse(fmt.Sprintf(basePath, userId, productId))
if err != nil {
    // Need to handle this error
}

You can do:

basePath := "https://example.com/users/{userId}/products/{productId}/"

baseURL := httpurl.MustParse(basePath)
httpurl.ExpandPath(baseURL, httpurl.ExpandMap{
  "userId": userId,
  "productId": productId,
})

About

Go library to make handling http URLs easier

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%