Home > front end >  How to properly send date param to axios in React
How to properly send date param to axios in React

Time:12-22

I wanted to send date as param to service with axios but when I send it goes as

messageStartDate=2021-12-21T11:47:54.810Z&messageEndDate=2021-12-21T11:47:59.718Z

I wanted to send as Iso format but there is %-A and some other things. How can I fix it.

this is how I store the date in redux.

const params = new URLSearchParams();
if (MessageDateStartValueRedux !== null)
        params.append('messageStartDate',new Date(MessageDateStartValueRedux).toISOString());
    if (MessageDateEndValueRedux !==null)
        params.append('messageEndDate',new Date(MessageDateEndValueRedux).toISOString());
    if (ValueDateStartValueRedux !== null)
        params.append('valueStartDate',new Date(ValueDateStartValueRedux).toISOString());
    if (MessageDateEndValueRedux !== null)
        params.append('valueEndDate',new Date(MessageDateEndValueRedux).toISOString());

    dispatch(setUrlParamsReducer(params));

and this is how I use it

const getQueryString = () => `${DEFAULT_URL}?page=${currentPage}&size=${pageSize}` status;

const param = useSelector(state => state.urlParamsReducer);

console.log(param.toString());
const loadData = () => {
    const queryString = getQueryString();
    if ((queryString !== lastQuery || param.toString() !== lastParam.toString()) && !loading) { 
        axios.get(queryString, {params: param})
            .then((response) => {
                setRows(response.data.data);
                setTotalCount(response.data.totalCount);
            }).catch(() => setLoading(false));
    }
}
useEffect(() => loadData(), [param,getQueryString()]);

CodePudding user response:

: is the url encoding of a ':' char. The decoded url is

messageStartDate=2021-12-21T11:47:54.810Z&messageEndDate=2021-12-21T11:47:59.718Z

as expected

  • Related