FedEx REST API上传文档遇503错误:脚本问题还是仅支持生产环境?
FedEx Upload Documents API 503 SERVICE.NOT.AVAILABLE 问题排查
我正在用Python测试FedEx REST API,已完成认证且能正常执行运单验证,但调用Upload Documents的POST请求时遇到503错误。FedEx官方文档缺少该接口的正确示例,当前收到的错误响应为:
{
"customerTransactionId": "12XXXXXX",
"errors":[
{
"code":"SERVICE.NOT.AVAILABLE",
"message":"Enterprise Document Upload Service Unavailable"
}
]
}
我的请求代码如下:
url = "https://documentapitest.prod.fedex.com/sandbox/documents/v1/etds/upload" f = open("file.txt", "rb") text_data = f.read() f.close() headers = { "Content-Type": "multipart/form-data", "x-customer-transaction-id": "12XXXXXX", "Authorization": f"Bearer {access_token}", } response = requests.post( url, files=dict( document='{"workflowName":"ETDPreshipment","name":"file.txt","contentType":"text/plain","meta":{"shipDocumentType":"COMMERCIAL_INVOICE","originCountryCode":"DK","destinationCountryCode":"BE"}', attachment=("file.txt", text_data, "text/plain") ), headers=headers, ) print(response.text)
可能的问题及解决方向
1. 沙箱环境权限与可用性
FedEx部分API的沙箱环境功能受限,Upload Documents API可能仅对生产环境开放:
- 先确认你的FedEx开发者账号是否已开通沙箱环境下的文档上传权限
- 若权限没问题仍报错,可尝试切换至生产环境端点(注意:生产环境会产生真实业务数据,操作前需确认业务需求)
2. 请求格式错误
你的代码存在两处明显问题:
- JSON语法不完整:
document字段的JSON字符串末尾缺少闭合的}},正确格式应为:{"workflowName":"ETDPreshipment","name":"file.txt","contentType":"text/plain","meta":{"shipDocumentType":"COMMERCIAL_INVOICE","originCountryCode":"DK","destinationCountryCode":"BE"}} - 手动设置Content-Type导致请求异常:使用
requests的files参数发送multipart请求时,库会自动生成包含boundary的Content-Type头部,手动设置"Content-Type": "multipart/form-data"会覆盖该自动生成的头部,导致服务器无法解析请求。需删除headers中的此字段。
修正后的代码:
import requests url = "https://documentapitest.prod.fedex.com/sandbox/documents/v1/etds/upload" with open("file.txt", "rb") as f: text_data = f.read() headers = { "x-customer-transaction-id": "12XXXXXX", "Authorization": f"Bearer {access_token}", } response = requests.post( url, files={ # 为document字段指定application/json类型,明确告知服务器数据格式 "document": (None, '{"workflowName":"ETDPreshipment","name":"file.txt","contentType":"text/plain","meta":{"shipDocumentType":"COMMERCIAL_INVOICE","originCountryCode":"DK","destinationCountryCode":"BE"}}', "application/json"), "attachment": ("file.txt", text_data, "text/plain") }, headers=headers, ) print(response.text)
3. 端点URL正确性
核对FedEx官方文档中的沙箱环境端点地址,确认documentapitest.prod.fedex.com是否为当前有效沙箱域名。
内容的提问来源于stack exchange,提问作者mfcss
相关产品推荐
相关产品推荐

