【问题标题】:Axios post request failed with status code 401Axios 发布请求失败,状态码 401
【发布时间】:2021-05-18 15:02:32
【问题描述】:

嗨,我试图根据课程代码从https://opentimetable.dcu.ie/ 中提取数据,例如 CASE3,所以我试图拉出几周的时间表,但我是 axios 的新手,我的帖子请求失败了,因为我相信的标题和我不知道为什么我认为我做对了?我基本上试图在下面的发布请求中获取 api 中的课程标识

    import React, { useState, useEffect } from 'react';
    import { View, Text } from 'react-native';
    import axios from 'axios';
    
    export default function LoginScreen() {
      const [user, setUser] = useState({});
    
      const Query = 'CASE3';
    
    
      useEffect(() => {
        async function loadAPI() {
          const response = await axios.post(`https://opentimetable.dcu.ie/broker/api/CategoryTypes/241e4d36-60e0-49f8-b27e-99416745d98d/Categories/Filter?pageNumber=1&query=${Query}`,{headers:{
            "Authorization": "basic T64Mdy7m[",
            "Content-Type" : "application/json; charset=utf-8",
            "credentials": "include",
            "Referer" : "https://opentimetable.dcu.ie/",
            "Origin" : "https://opentimetable.dcu.ie/"}
          });
    
          setUser(response.data);
        }
    
        loadAPI();
      }, []);
    
      return (
        <View style={{ alignItems: "center", marginTop: 24 }}>
          <Text>
            {user.bio}
          </Text>
      </View>
      );
    }

【问题讨论】:

标签: node.js react-native post axios


【解决方案1】:

401 错误状态响应代码表示请求缺少有效的身份验证凭据。所以我怀疑你的授权令牌。 然而,使用 postman我得到了 200 ok。所以令牌很好。所以我的猜测是授权失败是因为你传递标题的方式。无论如何,这里的代码,让我知道它是否有效。

const axios = require('axios');
useEffect(() => {
    const loadAPI = async () => {
        const config = {
            method: 'post',
            url: 'https://opentimetable.dcu.ie/broker/api/CategoryTypes/241e4d36-60e0-49f8-b27e-99416745d98d/Categories/Filter?pageNumber=1&query=CASE3',
            headers: { 
                'Authorization': 'basic T64Mdy7m['
            }
        };

        const response = await axios(config);
        console.log(response);  //just checking if works 
        setUser(response.data);
    }
    loadAPI();
}, []);

这是邮递员的回复: screenshot.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2022-06-25
    • 2020-04-05
    • 2021-04-07
    相关资源
    最近更新 更多