Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot POST datetime to API

I’m trying to POST datetime to my API but I keep getting this error:

raise TypeError(f'Object of type {o.__class__.__name__} '

TypeError: Object of type datetime is not JSON serializable

Code:

@Slot(int, int, int)
def postValues(self, getPostValue1, getPostValue2, getPostValue3):
    total = getPostValue1
    amountPaid = getPostValue2
    amountRemaining = getPostValue3
    customerID = 2
    userID = 3
    CurrentDateTime = datetime.datetime.now() 

    putData = {"customer_id": customerID, "total_amount": total, "amount_tendered": amountPaid, "date_recorded": CurrentDateTime, "user_id": userID, "amount_remaining": amountRemaining}

    post = requests.post("http://localhost:8085/api/invoice", json=putData)

    print("Invoice Data POST'ed", post.text)

I’ve looked at other questions highlighting the same issue, but I don’t understand how I can incorporate it in my code. Any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to convert the date-time value into a string. Change:

CurrentDateTime = datetime.datetime.now()

to:

CurrentDateTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading