Hello. I am sending a POST request to create a new mailbox using Axios.js in the following format:

const data = {
                    active: 1,
                    domain: "MY-DOMAIN",
                    local_part: username,
                    name: username,
                    password: invite,
                    quota: 1024,
                    force_pw_update: 1,
                    tls_enforce_in: 1,
                    tls_enforce_out: 1
                };

                const config = {
                    headers: {
                        "Content-Type": "application/json",
                        "Authorization": "MY-API-TOKEN"
                    }
                };

                axios.post('https://MYDOMAIN/api/v1/add/mailbox', data, config)
                .then(response => {
                    console.log(response.data);
                    if (response.status == 200) {
                        res.send(`blahblahblah`)
                        console.log(JSON.stringify(response.content)); // debugging feature
                    };
                })
                .catch(error => {
                    console.error(error);
                    res.status(500).send('blahblahblah');
                });

It does not return anything and the status code is 200, but the mailbox is not created. I’ve verified it’s nothing related to CORS.

Has this maybe happened to anyone/does anyone know what might be going on?

Don’t know axios, but here’s what ChatGPT has to say to your code:

The code you shared mostly looks good, but there are a few minor issues and suggestions for improvements:

  1. Incorrect Property in the Success Block:

    • In the .then() block, you log response.content, which doesn’t exist in Axios response objects. Axios responses use response.data for the response body, so this line should likely be console.log(JSON.stringify(response.data));.
  2. Header Token Usage:

    • The authorization header in the config object includes "Authorization": "MY-API-TOKEN". If your API requires the format Bearer <token>, make sure you prepend Bearer to the token.

         "Authorization": `Bearer MY-API-TOKEN`
  3. Response Code Check (Optional):

    • Instead of if (response.status == 200), it’s safer to use === (strict equality).
  4. Debugging Statements in Production:

    • If console.log statements are only for debugging, consider wrapping them in a condition to ensure they’re omitted from production builds, which is good practice for clean code.

Here’s your corrected code snippet with these adjustments:

const data = {
    active: 1,
    domain: "MY-DOMAIN",
    local_part: username,
    name: username,
    password: invite,
    quota: 1024,
    force_pw_update: 1,
    tls_enforce_in: 1,
    tls_enforce_out: 1
};

const config = {
    headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer MY-API-TOKEN`
    }
};

axios.post('https://MYDOMAIN/api/v1/add/mailbox', data, config)
    .then(response => {
        console.log(response.data);
        if (response.status === 200) {
            res.send('blahblahblah');
            console.log(JSON.stringify(response.data)); // corrected debugging feature
        }
    })
    .catch(error => {
        console.error(error);
        res.status(500).send('blahblahblah');
    });

Have something to say?

Join the community by quickly registering to participate in this discussion. We'd like to see you joining our great moo-community!

No one is typing