1

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

RAJA
  • 85
  • 1
  • 9
  • The decoded type is now double encoded rather than decoded. I think that Axios is encoding the request parameters, so it should work without `encodeURIComponent` in the query parameters. – adrenalin Nov 30 '20 at 19:12

2 Answers2

2

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'))
adrenalin
  • 1,656
  • 1
  • 15
  • 25
0

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.
Or Assayag
  • 5,662
  • 13
  • 57
  • 93
  • There is big advantage in what comes to readability when you use ISO 8601(ish) datetimes. Besides that your answer assumes that the one asking the question can change the API. – adrenalin Nov 30 '20 at 19:11
  • Sometimes you get other approaches that solves your problem :) – Or Assayag Nov 30 '20 at 19:13
  • You are assuming that it is even possible to change the server side, which is not described in the question – adrenalin Nov 30 '20 at 19:14