Fetch data POST HTTP Request - Javascript

Hi everyone,

I want to fetch data with HTTP request POST but it doesn’t work.

I receive a 422 code error when I send data since my app. But when I use Postman to send data, it works correctly.

Could you help me please ?

async function uploadDataToFreshSales() {
    try{

        const url = new URL(`https://${freshsalesDomain}/crm/sales/api/contacts`);
        

        const headersConfig = {
            'Authorization': `Token token=${freshsalesApiKey}`,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }

        const dataConfig = JSON.stringify({
            "contact":{
                "first_name": "John",
                "last_name": "Smith",
                "job_title": "Director",
                "sales_accounts": [
                    {
                        "id" : 12006351971,
                        "is_primary": true
                    }
                ],
                "territory_id": 12000002190,
                "email": "email@company.com",            
                "custom_field": {
                    "cf_civilit" : "Mr",
                    "cf_sector": "Retail"
                }
            }
        });

        const response = await fetch(url, {
            method: 'POST',
            headers: headersConfig,
            body: dataConfig
        });

        const responseData = await response.json();
        console.log(responseData);

    } catch(error){
        console.error('Error with FreshSales:', error);
    }
}

Hi @Marc

Welcome to the freshworks community… :dizzy:

Can you please use this request method to make API requests in Freshworks apps? For more information about the 422 error, click here.

Hi @Marc,

Welcome to the community :tada:

I tried the code with my credentials, and it works!

Could you please change the data in the payload and check again?

Thank you.

Hi,

I identified the problem.
The extension I coded doesn’t work on Edge but works correctly on Chrome.

So I thought that the problem was Edge and not my code. So I reset Edge Flags and restarted my computer and it works now.

I don’t think the problem was Edge Flags, but if you have the 422 error, we can try.

This is my code below for curious people.

document.getElementById('sendToFreshSales').addEventListener('click', () => {

    uploadDataToFreshSales();

});


// Config FreshSales
const freshsalesApiKey = '**************';
const freshsalesDomain = 'mycompany.freshworks.com';


async function uploadDataToFreshSales() {
    try{

        const url = new URL(`https://${freshsalesDomain}/crm/sales/api/contacts`);
        

        const headersConfig = {
            'Authorization': `Token token=${freshsalesApiKey}`,
            'Content-Type': 'application/json; charset=utf-8',
            'Accept': 'application/json'
        }

        const dataConfig = JSON.stringify({
            'contact':{
                'first_name': 'John',
                'last_name': 'Smith',
                'job_title': 'Director',
                'sales_accounts': [
                    {
                        'id' : 12006351971,
                        'is_primary': true
                    }
                ],
                'territory_id': 12000002190,
                'email': 'test@company.com',            
                'custom_field': {
                    'cf_civilit' : 'Mr',
                    'cf_sector': 'Retail'
                }
            }
        });

        const response = await fetch(url, {
            method: 'POST',
            headers: headersConfig,
            body: dataConfig
        });

        const responseData = await response.json();

        if (!response.ok) {
            console.error('Error with send data to FreshSales:', responseData);
            console.log('Response status:', responseData);
            console.log('Response data:', responseData);
        } else {
            console.log('Data received by FreshSales', responseData);
        }
    } catch(error){
        console.error('Communication error with FreshSales:', error);
    }
}

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.