Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Axios is not working to get access token?

i have axios call to get the bearer token it is oauth2.0 but for some reason it is failing same call is working in postman.

index.js

export async function getESLAccessToken(apiConfig: any) {
    const _body = {
        grant_type: apiConfig.authOptions.body.grant_type,
        client_id: apiConfig.authOptions.credentials.sdk_voyage.clientId,
        client_secret: apiConfig.authOptions.credentials.sdk_voyage.clientSecret,
        scope: apiConfig.authOptions.body.scope
    };
    const _headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "appName": "Blink",
        "Accept": "*/*",
        "Content-Length": 163
    };
    const request = {
        method: 'POST',
         _body,
        headers: _headers
    };

    try {
        const resp = await axios.post('https://auth/oauth2/token',
        request);
        console.log(resp.data);
    } catch (err) {
        // Handle Error Here
        throw err;
    }
}

This is how request building for axios

{
  "method": "POST",
  "_body": {
    "grant_type": "client_credentials",
    "client_id": "abc",
    "client_secret": "xyz",
    "scope": "APPPII APPPHI"
  },
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "appName": "Blink",
    "Accept": "*/*",
    "Content-Length": 163
  }
}

Error

Error: Request failed with status code 400

Postman working request and response

POST https://auth/oauth2/token
200
163 ms
POST /auth/oauth2/token HTTP/1.1

Headers
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Postman-Token: d90ccc33-1d77-4502-9a41-74080dd3d7a5
Host: qaapih8.corp.cvscaremark.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 163
Request Body
grant_type=client_credentials&client_id=abc&client_secret=xyz&scope=APPPII%20APPPHI
HTTP/1.1 200 OK

Response Body

{ "token_type":"Bearer", "access_token":"token returned", "expires_in":3600, "consented_on":164684, "scope":"APPPII APPPHI" }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The second argument of the axios.post function should be the request body. And the third argument should be the Axios request config object (Which you can set your headers). Axios POST Requests

In your case, the code should be like:

const body = {
  grant_type: apiConfig.authOptions.body.grant_type,
  client_id: apiConfig.authOptions.credentials.sdk_voyage.clientId,
  client_secret: apiConfig.authOptions.credentials.sdk_voyage.clientSecret,
  scope: apiConfig.authOptions.body.scope
};

const myHeaders = {
  // add your headers here
}

const response = await axios.post(
  'https://auth/oauth2/token',
  body,
  { headers: myHeaders }
);
about 4 years ago · Juan Pablo Isaza Report

0

Replace

const request = {
    method: 'POST',
     _body,
    headers: _headers
};

with:

const request = {
    method: 'POST',
    data: _body,
    headers: _headers
};

As pointed out by the docs in the comments session: https://axios-http.com/docs/req_config

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!