APIs & Integrations

MAlmeida4
Member

I can't test api endpoints on localhost

SOLVE

I implemented contact creation and I can't test it on localhost. It's giving me a CORS error.
I've tried several approaches and nothing works. Here's what I've tried so far:

 

 

 axios.defaults.headers.common['Authorization'] = `Bearer ${publicRuntimeConfig.ACCESS_TOKEN}`;
                    axios.defaults.headers.common['Content-Type'] = `application/json`;
                    axios.defaults.headers.common['Access-Control-Allow-Origin'] = `*`;
                    axios.defaults.headers.common['Access-Control-Allow-Headers'] = `*`;
                    axios.defaults.headers.common['Access-Control-Allow-Credentials'] = 'true';
                    axios.defaults.headers.common['Access-Control-Allow-Methods'] = 'GET,HEAD,OPTIONS,POST,PUT';

                    axios.post(`https://api.hubapi.com/crm/v3/objects/contacts`, props)
                        .then((e) => {
                            console.log('Response');
                        })
                        .catch(() => {
                            console.log('ERROR');
                            setErrorState();
                        });

 

 

 

 

 

 const endpoint = "https://api.hubapi.com/crm/v3/objects/contacts";

        const data = await fetch(endpoint, {
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${publicRuntimeConfig.ACCESS_TOKEN}`
            },
            method: 'POST',
            body: JSON.stringify(props)
        })
            .then(response => {
                console.log('response');
            })
            .catch(error => {
                console.log('ERROR');
                setErrorState();
            });

 

 

 

0 Upvotes
1 Accepted solution
Jaycee_Lewis
Solution
Community Manager
Community Manager

I can't test api endpoints on localhost

SOLVE

Hey, @MAlmeida4 👋 Regarding the CORS issue, that is the expected behaviour. You'll need to look into other solutions:

  • create a backend proxy 
  • utilize a service like ngrok
  • use Postman or CURL to make requests directly to the endpoint(s) you are working with

Have fun testing! — Jaycee

 


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !

View solution in original post

0 Upvotes
1 Reply 1
Jaycee_Lewis
Solution
Community Manager
Community Manager

I can't test api endpoints on localhost

SOLVE

Hey, @MAlmeida4 👋 Regarding the CORS issue, that is the expected behaviour. You'll need to look into other solutions:

  • create a backend proxy 
  • utilize a service like ngrok
  • use Postman or CURL to make requests directly to the endpoint(s) you are working with

Have fun testing! — Jaycee

 


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes