user4061275
user4061275

Reputation:

react axios get url encode decode issue

I am using Axios to get some data from the server based on the date range.

this is my sample Axios request

const params = {
  fromdate: encodeURIComponent(datefrom),
};    

axios
      .get(url, {params})

I am using the following method to encode the data

encodeURIComponent(_.toString(datefrom))

The server accepting the following patterns

 **Encoded Type:** fromdate: 2020-11-01%200%3A0%3A00

 **Decoded Type:** fromdate: 2020-11-01 0:0:00

But while I am passing to the server I am getting like that

 **Encoded Type:** fromdate: 2020-11-01%200%3A0%3A00

 **Decoded Type:** 2020-11-01%25200%253A0%253A00

How can I get the Decoded type like

Decoded Type: fromdate: 2020-11-01 0:0:00

instead of

Decoded Type: 2020-11-01%25200%253A0%253A00

Upvotes: 1

Views: 7222

Answers (2)

adrenalin
adrenalin

Reputation: 1658

Simplest way to solve this particular issue is to write the query manually rather than relying on Axios's own internals, which does not do anything wrong. Space can be described as + (more information here) in GET query. If you want to use explicit %20 then this is the fastest way for your query:

axios.get('/?fromdate=' + encodeURIComponent('2020-11-01 0:0:00'))

Upvotes: 2

Or Assayag
Or Assayag

Reputation: 6336

Let me offer you a different and maybe more simple way of passing dates from client to server, by timestamps (also refer as unix):

client:

const params = {
  fromdate: (new Date("2013/09/05 15:34:00").getTime()/1000),
};    

axios.get(url, {params})

and in the server:

const fromdate = new Date(req.params.fromdate * 1000);
// Parse it back to 2020-11-01 0:0:00 or whatever format you want.

Upvotes: 0

Related Questions