How to remove fragments from URIs?

Hello, I want to build a function to remove fragments from URIs, for example, it would convert http://example.com/faqs#one to http://example.com/faqs.

List.first(String.split(url, "#")) does the work, but I think something based on URI would be better, like:

s = "http://example.com/faqs#one"
u = URI.parse(s)
# %URI{authority: "example.com", fragment: "one", host: "example.com",
# path: "/faqs", port: 80, query: nil, scheme: "http", userinfo: nil}

# Here I need to remove the fragment from the parsed URL, but how to do this?

URI.to_string(u)
# "http://example.com/faqs#one"

How would you do this?

Thanks!

2 Likes
iex(2)> "http://example.com/faqs#one" |> URI.parse |> Map.put(:fragment, nil) |> URI.to_string
"http://example.com/faqs"
6 Likes

Thanks!