API returns Cloudflare 400 error for POST calls

I’m trying to add a contact to Hubspot and I’ve been following the guide. I wrote a script that runs a PHP script that uses a CURL request, but when I send anything with a POST request, I get a Cloudflare 400 error page.

There’s nothing special here. I’ve stripped my PHP script down to just the bare data (which closely mirrors the demo script in the docs), but no matter what data I send I get the same result.

Hey @OCG,

Do you mind sharing with me the code snippet and is this the exact endpoint that you’re using - Create a new contact | Contacts API?

Additionally, could you try POST-ing the request over Postman and see if it works?

I’ve tried with everything, even the stock PHP example on https://developers.hubspot.com/docs/methods/contacts/create_contact and all get a 400 error from Cloudfront

I ran this on our AWS server and I at least got a 400 with JSON. Running it in-house I only get a 400 HTML page from Cloudflare.

I’ll look into Postman.

Hey @OCG,

Do you have the full error message code that you can share with me? Additionally, what is the HubSpot portal ID in which you’re creating the contact on?

Please do let me know if it works on Postman!

I haven’t had a chance to fiddle with Postman. This is the full error from CURL

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

As I said previously, I do get a JSON error running it on an AWS server

{"status":"error","message":"Invalid JSON input: No content to map due to end-of-input at line 1, column 0","correlationId":"cc0b2d88-bffa-41ef-a18e-60e6641723a6","requestId":"31e53c5741a2fb2e2edfe7f63548012a"}

That could be due to the copy-paste over the command line, however.

Hey @OCG,

Can I check if you’re trying to create a new contact using this endpoint Create a new contact | Contacts API and the following POST body:

{
 "properties": [
 {
 "property": "email",
 "value": "testingapis@hubspot.com"
 },
 {
 "property": "firstname",
 "value": "Adrian"
 },
 {
 "property": "lastname",
 "value": "Mott"
 },
 {
 "property": "website",
 "value": "http://hubspot.com"
 },
 {
 "property": "company",
 "value": "HubSpot"
 },
 {
 "property": "phone",
 "value": "555-122-2323"
 },
 {
 "property": "address",
 "value": "25 First Street"
 },
 {
 "property": "city",
 "value": "Cambridge"
 },
 {
 "property": "state",
 "value": "MA"
 },
 {
 "property": "zip",
 "value": "02139"
 }
 ]
}

on portal 719xxxx?

If so, I have managed to successfully create a contact (which I have deleted) using the above POST body on portal 719xxxx.

Additionally, are you using a hapikey/oauth token to authenticate the request?

Just to be clear, I get no response at all. No JSON. If I make calls from my live server (even with your JSON) I get

Invalid JSON input: No content to map due to end-of-input at line 1, column 0

I am using a hapikey call

$curl = curl_init();
$options = [
 CURLOPT_URL => 'https://api.hubapi.com/contacts/v1/contact?hapikey=<key removed>',
 CURLOPT_RETURNTRANSFER => true
];

Hey @OCG,

Researching into the error, it seems that a simple fix is to add `Content-Type: application/json’ based on this stack overflow thread.

Could you try and see if this fixes it?

Sorry, was posting snippets. I already do that. Here’s a fuller picture

<?php
$curl = curl_init();
$options = [
	CURLOPT_URL => 'https://api.hubapi.com/contacts/v1/contact?hapikey=<key removed>',
	CURLOPT_RETURNTRANSFER => true
];

$options[CURLOPT_POST] = true;
$options[CURLOPT_HTTPHEADER] = ['Content-Type: application/json'];
$option[CURLOPT_POSTFIELDS] = '{"properties":[{"property":"email","value":"temp@somedomain.com"},{"property":"firstname","value":"Jack"},{"property":"lastname","value":"Smith"},{"property":"phone","value":"123-456-7890"},{"property":"address","value":"1234 My Street"},{"property":"city","value":"Mytown"},{"property":"state","value":"CA"},{"property":"zip","value":"91030"}]}';

curl_setopt_array($curl, $options);
$response = curl_exec($curl);
echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo $response;
echo curl_error($curl);

That code does not work with your supplied JSON from a previous answer, either.

I’m also getting the same error when try to clear contact properties and create activities. First API call becoms success but after that all calls gives below error. These are consecutive API calls runs one after the other (Clear fields and create activities).

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>