APIs & Integrations

npesa92
Member

Create Contact Access-Control-Allow-Origin error

I am trying to write webpage with a form that sends a contact directly into my HubSpot CRM. I have initially got the Access-Control-Allow-Origin error and I have seen now that it does not take cross origin requests and I have to send to my serve first then send to HubSpot, which I am fine configuring. If I enable an extension to bypass that in Chrome I am getting a 400 Bad Request Error when sending that data using the HubSpot Contact API.

Here is the error:

and here is the code giving me the error:

function createJSONArray() {

var formattedJson = {
	"properties":[
		{"property": "email", "value": "test"},
		{"property": "firstname", "value": "test"},
		{"property": "lastname", "value": "test"},
		{"property": "phone", "value": "test"},
		{"property": "zip", "value": "test"}
	]
}

var url = "https://api.hubapi.com/contacts/v1/contact/?hapikey=";
var params = "contact=" + formattedJson;
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhr.send(params);

}

If anyone has any help they could give me to solve this issue it is very appreciated, I am just trying to figure out how this API works between webpages. As well if you could answer one other questions: Would a java Servlet server work for this, or should I be using PHP?

Thanks in advance!

0 Upvotes
2 Replies 2
Dadams
HubSpot Employee
HubSpot Employee

Create Contact Access-Control-Allow-Origin error

Hi @npesa92

The Contacts API takes JSON formatted data, so the content-type would need to be application/json and not form encoded. The data you’re sending should also just be the stringified formattedJson variable

xhr.send(JSON.stringify(formattedJson))

However, as you mentioned, you’d need to make the actual request to HubSpot server-side, so even with that code sending the data to your server you’d still need to pass that along to HubSpot. The server-side language wouldn’t really matter, so either Java or PHP would work for that.

0 Upvotes
npesa92
Member

Create Contact Access-Control-Allow-Origin error

Thank you, I see now that I need to stringify the JSON.

One question: So if I use a server and send the data there and then over to HubSpot it will recognize that as an accepted connection. So can this come from a Google Server or a Firebase Hosted server?

0 Upvotes