我正在尝试将日期时间发布到我的 API,但我不断收到此错误:
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable
代码:
@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)
我查看了突出同一问题的其他问题,但我不明白如何将其合并到我的代码中。任何帮助,将不胜感激。
您需要将日期时间值转换为字符串。改变:
CurrentDateTime = datetime.datetime.now()
到:
CurrentDateTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句