euunit
November 30, 2019, 11:49am
1
Hey all,
I am new to HubSpot and build a little php class where I can add contacts and now I want to add notes to contacts but I get a “bad request”.
Here is my json code:
{
"engagement": {
"active": true,
"type": "NOTE",
"timestamp": 1575112787442
},
"associations": {
"contactIds": [
1351
],
"companyIds": [],
"dealIds": [],
"ownerIds": []
},
"metadata": {
"body": "Meine Notiz :-)!"
}
}
Which is build with this php code:
try {
$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
$lTimeStampNow = round(microtime(true) * 1000);
$lEngagement['engagement'] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
$lEngagement['associations'] = array( "contactIds" => array($pHubSpotContactId), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
$lEngagement['metadata'] = array("body" => "$pNoteText");
$lEngagementAsJSON = json_encode($lEngagement);
$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $lEngagementAsJSON
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
print_r($lEngagementAsJSON);
}
catch(Exception $e) {
return $e->getMessage();
}
I hope someone can find the mistake, I cant see it
Hello @euunit
Can you try replacing single quotes to double for the array indexes of engagement, association and metadata. Hope it can resolve the problem.
Thanks
euunit
November 30, 2019, 10:21pm
3
Hello @himanshurauthan
That didn’t helped me
My php Code now looks like that
try {
$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
$lTimeStampNow = round(microtime(true) * 1000);
$lEngagement["engagement"] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
$lEngagement["associations"] = array( "contactIds" => array($pHubSpotContactId), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
$lEngagement["metadata"] = array("body" => "$pNoteText");
$lEngagementAsJSON = json_encode($lEngagement);
$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $lEngagementAsJSON
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
print_r($lEngagementAsJSON);
}
catch(Exception $e) {
return $e->getMessage();
}
And the JSON lokks still like that
{
"engagement": {
"active": true,
"type": "NOTE",
"timestamp": 1575152265002
},
"associations": {
"contactIds": [
1351
],
"companyIds": [],
"dealIds": [],
"ownerIds": []
},
"metadata": {
"body": "Meine Notiz :-)!"
}
}
Hello @euunit
Can you please once check for this line in the code?
$lEngagement['metadata'] = array("body" => "$pNoteText");
I can not see the $pNoteText defined anywhere in the code. As its the required parameter in the API request so may be an empty value can be causing the Bad Request Error.
Thanks
euunit
December 2, 2019, 6:35am
5
Hey @himanshurauthan
I changed the parameters to hard coded text like that but nothing changed.
BUT! When i Take the JSON and put it with the access token for oauth 2 in Postman-programm, the note will be created :-O!!
So the Json is correct and my post-curl is wrong?
Json
{"engagement":{"active":true,"type":"NOTE","timestamp":1575268931219},"associations":{"contactIds":[1351],"companyIds":[],"dealIds":[],"ownerIds":[]},"metadata":{"body":"My Text"}}
try {
$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
$lTimeStampNow = round(microtime(true) * 1000);
$lEngagement["engagement"] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
$lEngagement["associations"] = array( "contactIds" => array(1351), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
$lEngagement["metadata"] = array("body" => "My Text");
$lEngagementAsJSON = json_encode($lEngagement);
$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $lEngagementAsJSON
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
print_r($lEngagementAsJSON);
}
catch(Exception $e) {
return $e->getMessage();
}
$note_details = array(
"engagement" => array(
"active" => true,
"type" => "NOTE",
"timestamp" => 5456563646
),
"associations" => array(
"contactIds" => array( 1234 ),
),
"metadata" => array(
"body" => "TESTING",
)
);
@euunit Try to use the above format once and see if anything works.
euunit
December 2, 2019, 7:17am
7
@himanshurauthan I tryed it but that wont work. I tryed this but that still does not work. But this is the json which works in postman. So it has to do something with the header maybe?
try {
$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
$lTimeStampNow = round(microtime(true) * 1000);
$lEngagementAsJSON = '{"engagement":{"active":true,"type":"NOTE","timestamp":5456563646},"associations":{"contactIds":[1351]},"metadata":{"body":"TESTING"}}';
$header = array("Content-Type: application/json; Authorization: Bearer {$this->AccessToken}");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $lEngagementAsJSON,
CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
print_r($lEngagementAsJSON);
}
catch(Exception $e) {
return $e->getMessage();
}
euunit
December 2, 2019, 7:29am
8
@himanshurauthan I dont know but my two last replys were deletet? I cant see them.
So the code from you dont work and when I put the json as string to the fields option it wont work but in postman it works.
Maybe its something with my header?
@euunit
Try using this for headers:
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$access_token
);
Thanks
euunit
December 3, 2019, 6:19am
10
@himanshurauthan you are genius! With this header array it works with the first function I wrote. Thank you so much man :-)!!
For those with similar problems here is my full function:
public function sendNoteToHubSpot($pHubSpotContactId, $pKundenID, $pNoteText, $pPathChange = '') {
try {
$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
$lTimeStampNow = round(microtime(true) * 1000);
$lEngagement = array(
"engagement" => array(
"active" => true,
"type" => "NOTE",
"timestamp" => $lTimeStampNow
),
"associations" => array(
"contactIds" => array($pHubSpotContactId)
),
"metadata" => array(
"body" => $pNoteText
)
);
$lEngagementAsJSON = json_encode($lEngagement);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$this->AccessToken
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $lEngagementAsJSON
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
catch(Exception $e) {
return $e->getMessage();
}
}
Hello @euunit
Good to hear that your code works now. All the best.
Thanks.