Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

javascript - Request for an API with Axios - Unauthorized

I'm trying to make the request for an API, using Axios:

const axios = require ("axios")
const httpsAgent = require('https-agent')
const https = require('https') 

const instance = axios ({
    httpsAgent: new https.Agent({
        rejectUnauthorized: false
    }),
    auth: {
        username: 'username' 
    }
})
axios.post("url_api").then(function(response){
    console.log(response.data)
}).then(function(response){
    console.log(response.data)
}).catch((e)=>{console.log(e)})

but it displays error 401:

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string

    response: {
    status: 401,
    statusText: 'Unauthorized',
...
    },
    data: 'Unauthorized'
  },
  isAxiosError: true,
  toJSON: [Function: toJSON]
}

Is there any more configuration to do? Insomnia/Postman works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
axios.post("url_api",body,header)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...