Home > Software engineering >  How to send current date time to Rest Api with put request in react js?
How to send current date time to Rest Api with put request in react js?

Time:10-04

I am using axios in react js to send data in put request. it was working perfectly but when I tried to send current datetime with put request its giving me error .

Bad Request: /buyer/OrdersAPI/14/
[03/Oct/2021 14:19:44] "PUT /buyer/OrdersAPI/14/ HTTP/1.1" 400 132

I have to post current date and time to rest api date time field

My code

const api = axios.create({
    baseURL: 'http://localhost:8000/buyer/'
})
updateOrderStatus = async(id,status) => {
        let data = new FormData();
        data.append('status',status );
        data.append('orderStartTime',Date())
        await api.put(`OrdersAPI/${id}/`, data);
        this.getNewOrders();
    }

if I console.log(Date()) its giving me Sun Oct 03 2021 14:28:59 GMT 0500 (Pakistan Standard Time)

CodePudding user response:

The problem is that you pass a Date object so to fix that you need to convert it to json format like this

new Date().toJSON()

this will return a result like this

console.log(new Date().toJSON())

  • Related