Got a 'How do I' question? Join our live 'Ask a Trainer' session happening now for instant help and pro tips!

Invalid content type. API only supports application/json

SOLVED
Go to solution
ATMH1
Rookie
0 Votes

Hi Experts, 

I am getting the following response while trying to create a new contact using the API.

[{"error_key":"http.header.content_type.invalid","error_message":"Invalid content type. API only supports application/json."}]

 

I am trying to follow this instruction (https://developer.constantcontact.com/docs/contacts-api/contacts-collection.html?method=POST#example...)

 

For more information I am sharing my custom codes it is in PHP:

$curl = curl_init();
$post_string = array(
"address_type" => "BUSINESS",
"city" => "Belleville",
"country_code" => "CA",
"line1" => "47 Shawmut Ave.",
"line2" => "Suite 404",
"line3" => "",
"postal_code" => "K8b 5W6",
"state_code" => "ON",
'addresses' => array(
"address_type" => "PERSONAL",
"city" => "Cambridge",
"country_code" => "US",
"line1" => "541 Brighton Ave.",
"line2" => "Unit 785",
"line3" => "",
"postal_code" => "01555",
"state_code" => "MA",
"sub_postal_code" => ""
),
'lists' => array(
"id" => "1"
),
"cell_phone" => "555-555-5555",
"company_name" => "System Optimzations",
"confirmed" => false,
"email_addresses" => array(
"email_address" => "username1@example.com"
),
"fax" => "555-555-5555",
"first_name" => "Ronald",
"home_phone" => "555-555-5555",
"job_title" => "Systems Analyst 3",
"last_name" => "Martone",
"middle_name" => "Angelo",
"prefix_name" => "Mr.",
"work_phone" => "555-555-5555"
);
$header_content = array(
"authorization: Bearer <accessToken>",
"cache-control: no-cache"
);
$post_string = json_encode($post_string);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.constantcontact.com/v2/contacts?api_key=<api_key>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $post_string,
CURLOPT_HTTPHEADER => $header_content
));
$response = curl_exec($curl);
$err = curl_error($curl);
// var_dump($response);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Please suggest.

 

Thanks

-Hasan

 

1 ACCEPTED SOLUTION
Jimmy_D
Employee
0 Votes

Hi @ATMH1,

 

It looks like your header is not complete. You are missing the content type. Consider adding something like this to your header array.

 

"Content-Type:application/json"

Regards,
Jimmy D.
Tier II API Support Engineer

View solution in original post

1 REPLY 1
Jimmy_D
Employee
0 Votes

Hi @ATMH1,

 

It looks like your header is not complete. You are missing the content type. Consider adding something like this to your header array.

 

"Content-Type:application/json"

Regards,
Jimmy D.
Tier II API Support Engineer
Resources
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up