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

使用Axios调用Fedex追踪API遇阻,官方文档难理解求排查

FedEx追踪API的Axios调用问题

我尝试用Axios获取FedEx包裹的追踪产品信息,但官方文档难以理解。以下是我的代码:

import axios from "axios"
( ()=>{
const config = {
    method:`post`,
    url: "https://apis-sandbox.fedex.com/track/v1/trackingnumbers",
    headers:{
    "Content-Type" : "application/json",
    "X-locale": "en_US",
    "Authorization":"Bearer blabla"
}
data:JSON.stringify(<trackingNumber>)

}
 const  axi =   axios(config).then(res=> console.log(res.data))
    

})()

测试追踪号为:277607960166,请问我遗漏了什么内容?


问题分析与修正点

  • 请求体格式不符合API要求:FedEx Track API要求请求体是包含trackingNumbers数组的JSON对象,你直接传入<trackingNumber>是无效的。正确的请求体结构应该是:

    data: JSON.stringify({ trackingNumbers: ["277607960166"] })
    

    API支持一次传入多个追踪号,所以必须用数组包裹单个或多个追踪号。

  • 代码语法错误:config对象中,headers的闭合花括号}后面缺少逗号,导致data属性无法被正确解析。修正后的config结构:

    const config = {
        method: `post`,
        url: "https://apis-sandbox.fedex.com/track/v1/trackingnumbers",
        headers: {
            "Content-Type": "application/json",
            "X-locale": "en_US",
            "Authorization": "Bearer blabla"
        }, // 补上此处逗号
        data: JSON.stringify({ trackingNumbers: ["277607960166"] })
    }
    
  • 验证Bearer Token有效性:确保Authorization头中的blabla是从FedEx沙箱环境获取的有效访问令牌,无效令牌会直接导致认证失败。

修正后的完整代码示例:

import axios from "axios"

(() => {
    const config = {
        method: `post`,
        url: "https://apis-sandbox.fedex.com/track/v1/trackingnumbers",
        headers: {
            "Content-Type": "application/json",
            "X-locale": "en_US",
            "Authorization": "Bearer 你的有效沙箱令牌"
        },
        data: JSON.stringify({ trackingNumbers: ["277607960166"] })
    }

    axios(config)
        .then(res => console.log(res.data))
        .catch(err => console.error("请求失败:", err.response?.data || err.message))
})()

内容的提问来源于stack exchange,提问作者Foxsnow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 15:15:46