camcaine

camcaine

URI.merge/2 overwrites the query field. I’m assuming this is by design/spec?

Right now I’m appending query params like so:

Map.update(uri, :query, nil, fn 
  q when is_binary(q) -> q <> "&" <> URI.encode_query(params)
  _ -> URI.encode_query(params)
end)

Am I missing a different way of merging query strings?
Thanks!!

Showing Posts 1 to 3

peerreynders

peerreynders

Correct: 5.2.2. Transform References

               if defined(R.query) then
                  T.query = R.query;
               else
                  T.query = Base.query;
               endif;

i.e. the official algorithm uses the Base URI query parameters only if the Reference URI doesn’t have any query parameters - otherwise the Reference URI query parameters are used while the ones from the Base are ignored.

Am I missing a different way of merging query strings?

I’d be inclined to manage this situation a bit differently:

iex(1)> uri = "http://example.com/path/to/page?name=ferret&color=purple" |> URI.parse()
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80, 
  query: "name=ferret&color=purple",
  scheme: "http",
  userinfo: nil
}
iex(2)> query_decoded = URI.decode_query(uri.query)                         # use a Map instead of string
%{"color" => "purple", "name" => "ferret"}
iex(3)> my_uri = uri |> Map.from_struct() |> Map.put(:query, query_decoded) # Use a Map instead of URI struct 
%{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80,
  query: %{"color" => "purple", "name" => "ferret"},
  scheme: "http",
  userinfo: nil
}
iex(4)> query_key = Access.key(:query, %{})                                 # returns ":query" value or empty map if missing
#Function<4.127282935/3 in Access.key/2>
iex(5)> query_merge = fn uri, params ->
...(5)>    merge_query_params = &(Map.merge(&1, params))                    # params is a map to accept more than one param; last name/value wins for duplicate names
...(5)>    update_in(uri, [query_key], merge_query_params)
...(5)> end
#Function<12.127694169/2 in :erl_eval.expr/5>
iex(6)> no_query = Map.delete(my_uri, :query)                               # remove existing query params
%{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80,
  scheme: "http",
  userinfo: nil
}
iex(7)> my_weasel = query_merge.(no_query, %{name: "weasel"})               # add the first query params
%{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80,
  query: %{name: "weasel"},
  scheme: "http",
  userinfo: nil
}
iex(8)> my_green = query_merge.(my_weasel, %{color: "green"})               # append more query params
%{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80,
  query: %{color: "green", name: "weasel"},
  scheme: "http",
  userinfo: nil
}
iex(9)> query_encoded = URI.encode_query(my_green.query)                    # put it all together at the latest possible moment
"color=green&name=weasel"
iex(10)> new_my_uri = Map.put(my_green, :query, query_encoded)
%{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80,
  query: "color=green&name=weasel",
  scheme: "http",
  userinfo: nil
}
iex(11)> new_uri = struct(URI, new_my_uri)                                   # dump map contents into URI struct
%URI{
  authority: "example.com",
  fragment: nil,
  host: "example.com",
  path: "/path/to/page",
  port: 80, 
  query: "color=green&name=weasel",
  scheme: "http",
  userinfo: nil
}
iex(12)> URI.to_string(new_uri)
"http://example.com/path/to/page?color=green&name=weasel" 
iex(13)> 
camcaine

camcaine OP

Thanks for the alternative approach. I did explore decoding the query also, and might go back to that as it feels a little more declarative.

Parsed %URI{}'s without a query string return nil under that field, so using decode_query/1 would need to take that into account.

peerreynders

peerreynders

Fair point.

The high level takeaway should be that while URI contains “Utilities for working with URIs”, it may not perfectly suit your needs. So it would make sense to create your own module (UriParts?) and perhaps struct that supports the way you need it to operate - while at the same time internally making full use of the URI module whenever possible (and defdelegate/2 when appropriate).

Having

# uri_parts = UriParts.parse(uri_string)
# uri_parts = UriParts.from_uri(uri)

uri_parts = UriParts.query_merge(uri_parts, params)

# uri_string = UriParts.to_string(uri_parts)
# uri = UriParts.to_uri(uri_parts)

is preferable to having

Map.update(uri, :query, nil, fn 
  q when is_binary(q) -> q <> "&" <> URI.encode_query(params)
  _ -> URI.encode_query(params)
end)

scattered all over the codebase.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews