Invalid content type. API only supports application/json

SOLVED
Highlighted
Occasional Visitor

Invalid content type. API only supports application/json

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

 

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Invalid content type. API only supports application/json

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
Highlighted
Moderator

Re: Invalid content type. API only supports application/json

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

Developer Portal

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

Visit Page

Register for our "Making Sense of Online Marketing for Retail" Webinar

Join us on Thursday, August 13th at 2pm ET for a free webinar where we'll discuss how to incorporate online marketing into your retail store.

Read More
Featured