how to get special characters in URL parameters by searchParams.get()

Chinmay
1 min readOct 9, 2022

--

JavaScript Daily

sample url:
https://xyz.herokuapp.com/project-invitations/role?projectInvitationId=000&projectName=TestProject&token=abc123&inviteeEmail=cvkude+1@gmail.com

focus on inviteeEmail parameter which has a value equal to cvkude+1@gmail.com

let’s assume we need to get the inviteeEmail parameter from the URL, we can do so by using searchParams.get(“inviteeEmail”)

  • where using searchParams.get() is which is derived from `react-router-dom`

const inviteeEmail = searchParams.get(“inviteeEmail”);

in the above line of code if we console.log(inviteeEmail) then you would see output to be equal to cvkude 1@gmail.com and not cvkude+1@gmail.com

do you notice something unusual? The + symbol was replaced with the empty space “ ”.

the reason is, you need to encode the query parameters before combining them to form a url.

so if you pass the encoded parameter inviteeEmail=cvkude%2B1%40gmail.com then the below code would return the correct output viz. cvkude+1@gmail.com

const inviteeEmail = searchParams.get(“inviteeEmail”);

console.log(inviteeEmail) // cvkude+1@gmail.com

if you liked this article or found it useful, a follow would be much appreciated. Alternatively, you could buy me a coffee! All the support is much appreciated. :-)

https://www.linkedin.com/in/chinmay-kude

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Chinmay
Chinmay

Written by Chinmay

I often describe myself as a software professional having the mindset to break things but the toolset to create and restore.

No responses yet

Write a response