'Cannot POST datetime to API [closed]
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.
Solution 1:[1]
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cody Gray |
