You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Xero发票API时DueDate格式转义导致JSON数据无效的问题求助

调用Xero发票API时DueDate格式转义导致JSON数据无效的问题求助

我现在在用Python的httpx调用Xero发票API创建发票,但遇到了JSON数据无效的错误,折腾了好一阵没搞定,来求助大家!

我写了一个异步的创建发票函数,代码如下:

async def create_invoice(
        invoice_detail: InvoiceCreateRequest,
        token_manager=Depends(oauth_manager)
) -> InvoiceCreateResponse:
    base_url = 'https://api.xero.com/api.xro/2.0/Invoices'
    headers = {
        'Xero-Tenant-Id': get_settings().TENANT_ID,
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {await token_manager.access_token}'
    }
    data = invoice_request_to_dict(invoice_detail)

    async with httpx.AsyncClient() as client:
        response = await client.request(
            method='POST',
            url=base_url,
            headers=headers,
            data=data
        )

生成的data对象结构如下:

{
        "Type": "ACCREC",
        "Contact": {
            "ContactID": "3ed357da-0988-4ea1-b1b7-96829e0dde69"
        },
        "DueDate": r"\/Date(1518685950940+0000)\/",
        "LineItems": [
            {
                "Description": "Services as agreed",
                "Quantity": "4",
                "UnitAmount": "100.00",
                "AccountCode": "200"
            }
        ],
        "Status": "AUTHORISED"
    }

但每次调用都会返回以下错误:

b'{\r\n  "ErrorNumber": 14,\r\n  "Type": "PostDataInvalidException",\r\n  "Message": "Invalid Json data"\r\n}'

我排查下来觉得问题出在DueDate的格式上——Xero用的是.NET风格的日期格式,但Python会自动给字符串里的\加上转义符,最终发送的DueDate会变成'\\/Date(1518685950940+0000)\\/'。

我用Postman做了测试:直接用正确格式的payload调用同一个接口是可以成功的,但如果把Postman里的DueDate改成带多余转义符的版本,就会触发和代码里一样的错误:

JSON for post data was invalid,Could not convert string to DateTime: \/Date(1518685950940+0000)\/. Path 'DueDate', line 6, position 45.</Message>

我试了好几种方法都没法去掉这些多余的转义符,有没有朋友知道这种情况该怎么处理?翻了Xero的开发文档也没找到相关说明,实在头疼!

备注:内容来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:54:27