1

I use the following code using request which works as expected, got http response 200

var request = require('request');

var auth
var options = {
    'method': 'POST',
    'url': 'https://oauth2.k.de.com/oauth2/token',
    'headers': {
        'Accept': 'application/json',
        'Authorization': 'Basic NGViMTE2ODctZTNjNi00NDUyLTgwNjgtMzhiOjJDR2lJd0hxOFFx==',
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    form: {
        'grant_type': 'client_credentials',
        'scope': 'app:read'
    }
};
request(options, function (error, response) {
    if (error) throw new Error(error);

    let body = JSON.parse(response.body);

 ….

Now I need to convert it to axios as request been deprecated but it’s not working for me ( I got http 400 response )

const axios = require('axios').default;


axios({
    method: 'post',
    'url': 'https://oauth2.k.de.com/oauth2/token',
    data: {
        'grant_type': 'client_credentials',
        'scope': 'app:read'
    },
    headers: {
        'Accept': 'application/json',
         'Authorization': 'Basic NGViMTE2ODctZTNjNi00NDUyLTgwNjgtMzhiOjJDR2lJd0hxOFFx==',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function (response: any) {
    console.log("Head With Authentication :" + response);
}).catch(function (error: any) {
    console.log("Post Error : " + error);
});

Any idea why with request library with the exact same data it works (http response 200) and in axios I got 400 ? In request I put the grant_type etc in form and in axios in data, this is the only diffrencace I see, any idea?

This is the error I got

Request failed with status code 400

Should I use other rest libary if it cannot be done via axios ?

4
  • You've put the option element "url" in as string. I am not at my IDE, so I cannot verify if that's the problem. Commented Jun 2, 2020 at 15:10
  • grant_type and scope is not part of data. you should append them to headers I guess Commented Jun 2, 2020 at 15:11
  • @DirkR - should I try to change something? Commented Jun 2, 2020 at 15:13
  • @ilkerkaran - I've tried to put it on the header and got the same error, Request failed with status code 400 Commented Jun 2, 2020 at 15:14

1 Answer 1

2

This is a bug, you might want to check this: https://github.com/axios/axios/issues/362

The problem is, because of axios interceptors, your Content-Type header is disappearing. If you have access and can change the backend, you can make it work with another header and set it on your client code. Otherwise if your code is working in a browser, you can try using URLSearchParams as suggested here.

Sign up to request clarification or add additional context in comments.

4 Comments

so If I cannot change the BE , I cannot use axios ?
Yes. I checked the GitHub issues and axios guys won’t change their library. If you don’t want to change the backend, you can consider using another client library instead of axios.
Thanks for this! do you have any suggestion which may work for my use case?
I'm using fetch, but it's only for browsers, so it won't work in nodejs backend: developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.