Aatish
Aatish

Reputation: 106

Token authentication with C# Web Api and axios

I am implementing a login. I can send a post request to the endpoint token in Postman but not in axios.

Axios function:

axios({
    method: 'post',
    url: 'http://localhost:20449/token',
    headers: {
      'Content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      'grant_type': 'password',
       'username': user.username,
       'password': user.password
    }
}).then(resp => {
   console.log(resp)
   commit(AUTH_SUCCESS, resp)
   dispatch(USER_REQUEST)
   resolve(resp)
})

I get the error

"unsupported_grant_type"

Upvotes: 0

Views: 913

Answers (1)

Aatish
Aatish

Reputation: 106

I found a solution. Axios uses application/json by default when data is an object. It did not worked even after adding application/x-www-form-urlencoded in header. So I downloaded the package qs (npm install qs --save). I imported the package and use the axios command below:

var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 });

Upvotes: 1

Related Questions