Error 401 for authentication on the axelor REST API

125 Views Asked by At

I am currently trying to test the Axelor REST API, the problem is that I have a 401 error, even with their documentation it does not work, I manage to have access to a CSRF-TOKEN that I enter the header but still nothing can someone help me. here is my code

    <script>
    function sendLoginRequest() {
        return new Promise((resolve, reject) => {
            const data = {
                username: "admin",
                password: "admin"
            };

            $.ajax({
                url: 'http://localhost:8080/axelor-erp-6.4.14/login.jsp',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data),
                success: function(response, textStatus, jqXHR) {
                    const token = jqXHR.getResponseHeader('X-CSRF-Token');
                    resolve(token);
            },
            error: function(xhr, status, error) {
                console.log('Erreur lors de la requête. Statut:', xhr.status);
                console.log('Réponse serveur:', xhr.responseText);
                reject(error);
            }
            });
        });
    }


    sendLoginRequest()
            .then(token => {
                console.log('Token retourné:', token);
                getPartnerRequest(token);
            })
            .catch(error => {
                console.error(error);
            });

    function getPartnerRequest(token) {
        $.ajax({
`                url: 'http://localhost:8080/axelor-erp-6.4.14/ws/rest/com.axelor.apps.base.db.Partner/search',
                type: 'POST',
                headers: { 'X-CSRF-Token': token },
                data: JSON.stringify({
                    fields: ["fullName"],
                    limit: 20
            }),
            contentType: 'application/json'
        }).done(response => {
            const contactNames = response.data.map(e => e.fullName);
            const contactList = $('#contact-list');

            contactNames.forEach(name => {
                    $('<li>').text(name).appendTo(contactList);
            });
        });
    }`
    </script>
</body>
</html>`
0

There are 0 best solutions below