Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to specify encoding strategy for query params #558

Merged

Conversation

RudolfMan
Copy link
Contributor

@RudolfMan RudolfMan commented Feb 3, 2023

I was adapting Tesla in a project when we encode query params with :rfc3986 and I noticed that, while Tesla provides conveniences to build url encoding query params for us, it doesn't allow to specify the encoding strategy.

TL;DR

This PR allows specifying option query_encoding: :rfc3986 so that whitespaces in query params will be encoded as "%20".

Tesla.get("http://example.com", query: [username: "John Smith"], opts: [query_encoding: :rfc3986])

will build the following url

http://example.com?username=John%20Smith

Also adds optional argument to Tesla.build_url and Tesla.encode_query.


Tesla uses URI.encode_query/2 to encode query params. Since Elixir 1.12 that function allows to specify the encoding strategy.

You can specify one of the following encoding strategies:

  • :www_form - (default, since v1.12.0) keys and values are URL encoded as per encode_www_form/1. This is the format typically used by browsers on query strings and form data. It encodes " " as "+".

  • :rfc3986 - (since v1.12.0) the same as :www_form except it encodes " " as "%20" according RFC 3986. This is the best option if you are encoding in a non-browser situation, since encoding spaces as "+" can be ambiguous to URI parsers. This can inadvertently lead to spaces being interpreted as literal plus signs.

Encoding defaults to :www_form for backward compatibility.

@yordis
Copy link
Member

yordis commented Feb 3, 2023

Related to: #538

lib/tesla.ex Outdated Show resolved Hide resolved
@yordis
Copy link
Member

yordis commented Feb 3, 2023

Thank you so much for taking the time for this. I appreciate it.

@yordis
Copy link
Member

yordis commented Feb 16, 2023

Any final thoughts related to our conversation?

@yordis yordis force-pushed the master branch 6 times, most recently from 2bca420 to fe7207c Compare October 24, 2024 16:11
RudolfMan and others added 2 commits October 24, 2024 17:28
Co-authored-by: Yordis Prieto <yordis.prieto@gmail.com>
@yordis yordis force-pushed the allow_query_params_encoding_strategy branch from 7a3e513 to 715b75f Compare October 24, 2024 21:28
@yordis yordis force-pushed the allow_query_params_encoding_strategy branch from 06906ef to c267bbd Compare October 24, 2024 21:48
@yordis yordis changed the title Allow to specify encoding strategy for query params feat: allow to specify encoding strategy for query params Oct 25, 2024
@yordis yordis self-requested a review October 25, 2024 00:03
@yordis yordis requested a review from teamon October 25, 2024 01:53
@yordis
Copy link
Member

yordis commented Oct 25, 2024

@teamon share some love over here! Is the function changing from arity 2 to arity 3 with the default value any concern here?

Copy link
Member

@teamon teamon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all backwards compatible, looks good to me!

@yordis yordis merged commit 6f0f2c0 into elixir-tesla:master Oct 25, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants