Nodejs create diffrent post request

You’re nearly there.

The main problem is that the axios function needs to be called multiple times in the Promise.all function.

With the above solved, my last recommendation is to make separate form-data objects for each request.

For example:

var formData1 = getFormDataObjectSomehow()
var formData2 = getAnotherFormDataObjectSomehow()

let [res1, res2] = await Promise.all([
    axios({
        method: 'post',
        url: 'https://oauth2.md/oauth2/token',
        headers: {
            'Authorization': 'Basic [userpassword]',
            ...formData1.getHeaders()
        },
        data: formData1
    }),
    axios({
        method: 'post',
        url: 'https://oauth2.md/oauth2/token',
        headers: {
            'Authorization': 'Basic [userpassword]',
            ...formData2.getHeaders()
        },
        data: formData2
    })
])

console.log(res1.data.access_token)

EDIT: And don’t forget to handle errors thrown from the axios requests. Either add .catch() block to your Promise.all or use a try/catch guard.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top