未在 GET 请求中设置 Axios 标头

Axios headers not being set in GET request

提问人:Edim Zdralovic 提问时间:11/13/2023 更新时间:11/13/2023 访问量:18

问:

我在前端使用 axios 作为我的请求,使用 FastAPI 作为我的 Python 后端。我正在尝试添加授权标头,但它不知何故不起作用。当设置标头时,它永远不会出现,甚至在 axios 请求本身中也不会出现:

Request Details Chrome

我是这样构建我的请求的:

async get_species_list() {
  if (this.species_list !== null) return this.species_list;

  const userStore = useUserStore();
  const config = {
    headers: {
      Authorization: `Bearer ${userStore.token}`,
    },
  };
  try {
    const response = await axios.get(
      `${API_BASE_URL}${SPECIES_DICT_ENDPOINT}`,
      config
    );
    console.log(response.data.headers); // this yields 'undefined'

    const speciesAndBreeds = response.data; 
  } catch (error) {
    console.error("Error fetching species and breeds:", error);
  }
},

在我的后端,我有:

...

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost",
        "http://localhost:8080",
        "http://localhost:3000",
        "http://127.0.0.1",
        "http://127.0.0.1:8080",
        "http://127.0.0.1:3000",
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],

...

@app.get("/species-breeds")
async def get_species_list(header=Header(default=None)):
    print(header) # which just print 'None'
)
Python Axios 请求 标头 令牌

评论


答: 暂无答案