Home > Software design >  content-type header missing or unsupported
content-type header missing or unsupported

Time:09-01

I get this error, when I want to get a new refresh token from autodesk. https://forge.autodesk.com/en/docs/oauth/v2/reference/http/refreshtoken-POST/

 developerMessage: 'content-type header missing or unsupported content-type used, must use application/x-www-form-urlencoded'
bim360Router.use(async (req, res, next) => {
    //verify if forge token is set in the cookies and not expired
    let accessToken = req.cookies['access_token'];
    let refresh_token = req.cookies['refresh_token'];
    if(refresh_token && !accessToken) { //if token is expired,  then generate a new token if refresh token is set 
         const config ={
            headers : {
                'content-type': 'application/x-www-form-urlencoded',
            },        
            params : {
                client_id: process.env.FORGE_CLIENT_ID,
                client_secret: process.env.FORGE_CLIENT_SECRET,
                refresh_token: refresh_token,
                grant_type: 'refresh_token',
            }
        };
        const response = await axios.post(
            'https://developer.api.autodesk.com/authentication/v1/authenticate',
             config
        );
        accessToken = await response.data.access_token;
        //set access token as a cookie for session
        res.cookie('access-token', accessToken, { maxAge: 60 * 60 * 1000, httpOnly: true });
        console.log("middleware", response.data);
    }
    next();

});

I also try with headers : { 'Content-Type': 'application/x-www-form-urlencoded', }, but I still get the same error. (But when I try with curl it's working).

What I'm missing ?

CodePudding user response:

I solved this problem by doing this. I have absolutely no idea why it works but if anyone can answer. I find this code confusing.


            const response = await axios.post(
                'https://developer.api.autodesk.com/authentication/v1/refreshtoken',
                new URLSearchParams({
                    'client_id': process.env.FORGE_CLIENT_ID,
                    'client_secret': process.env.FORGE_CLIENT_SECRET,
                    'grant_type': 'refresh_token',
                    'refresh_token': refresh_token,
                })
            );

CodePudding user response:

try sending payload url-encoded

const params = new URLSearchParams();
params.append('client_id', process.env.FORGE_CLIENT_ID);
// ... other params
axios.post(..., params);

From autodesk docs:

Request Body Structure

The request body is a URL-encoded string of ampersand-concatenated, name-value pairs of the following parameters:

  • Related