I'm building a dashboard that will pull in data via an HTTP request. In order to get the data, I have to include date based "start" and "end" search params in the URL, so I decided to use URLSearchParams to allow this to be somewhat dynamic.
However, when I pass in the URLSearchParams into the final request they're getting altered. The basics layout of my code is as follows:
// create new date as of "today", and one week previous
const today = new Date()
const prevWeek = new Date(today.valueOf() - (86400000 * 7))
// get dates in UTC format for use in params
function getUTCDate(date) {
const utcDay = date.toISOString()
return utcDay
}
// creating params based on dates
const paramObj = {start: getUTCDate(prevWeek), end: getUTCDate(today)}
const params = new URLSearchParams(paramObj)
// setting up URL for HTTP call
const mailboxUrl = new URL(`https://api.helpscout.net/v2/reports/email?${params}`)
const config = {headers: {authorization: ""}}
If I log params
I get URLSearchParams { 'start' => '2020-11-17T21:59:20.459Z', 'end' => '2020-11-10T21:59:20.459Z' }
which has the correct formatting.
However when logging the final URL
I get 'https://api.helpscout.net/v2/reports/email?start=2020-11-17T21%3A58%3A52.627Z&end=2020-11-10T21%3A58%3A52.627Z'
Basically the colons are getting replaced with ISO character codes... but I'm not exactly sure why that is. I'm receiving a 400 error when making this request, so that's really the issue that I'm trying to resolve