APIs & Integrations

YGhimire
メンバー

Getting 400 Bad Request

解決

Hi there, 

I am trying to do CRM Bulk Import, for that, I am using "hubspot/api-client" and I am authenticating with "api-key".

 

But I am always getting "400 Bad Request" as a response.

 

FYI, this script is working if I use OAuth instead of API key but I want to use API Key

 

I couldn't figure out why it's not working with the API key, any help would be highly appreciated. 

 

Here goes my code: 

if(file_exists('src/files/formatted/contact.csv')) {
try {
$hubSpot = Factory::createWithApiKey($this->key);
$request = json_encode([
"name" => "Import (Contact)",
"files" => [
[
"fileName" => "contact",
"fileImportPage" => [
"hasHeader" => true,
"columnMappings" => [
[
"columnName" => "First Name",
"propertyName" => "firstname",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Contact Email Domain",
"propertyName" => "contact_email_domain",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Email",
"propertyName" => "email",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Company name",
"propertyName" => "company",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Country/Region",
"propertyName" => "country",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Full Name",
"propertyName" => "full_name",
"columnObjectType" => "CONTACT",
],
[
"columnName" => "Phone number",
"propertyName" => "phone",
"columnObjectType" => "CONTACT",
],
],
],
],
],
]);

$hubSpot->crm()->imports()->coreApi()->create('src/files/formatted/contact.csv', $request);
@unlink('src/files/formatted/contact.csv');
} catch (ApiException $e) {
echo "Exception when calling core_api->create: ", $e->getMessage();
}
}

## contact.csv

firstname,contact_email_domain,email,company,country,full_name,phone
Test,test.com,test@test.com,Test,GB,Test Test,0123456

 

0 いいね!
1件の承認済みベストアンサー
YGhimire
解決策
メンバー

Getting 400 Bad Request

解決

Hey there, 

 

I fixed my issue, it was something to do with the "filename" in the request.

Before:

"fileName" => "contact",

 

After:

"fileName" => "contact.csv",

 

That's how I fixed it.  It literally cost of a number of hours before I find out. 

 

I hope this would help somebody out there who faced the same issue. 

 

LG

Yub

元の投稿で解決策を見る

6件の返信
jtribble
参加者

Getting 400 Bad Request

解決

I ran into this as well. My POST /crm/v3/imports request was succeeding in Postman but failing when I tried to send it via the Go http package.

 

In my case, I forgot to call multipart.Writer.Close() before building my request! So my request body was missing the trailing boundary end line.

 

It would be helpful if the Hubspot API response contained a detailed error message in the event of a 400.

0 いいね!
YGhimire
解決策
メンバー

Getting 400 Bad Request

解決

Hey there, 

 

I fixed my issue, it was something to do with the "filename" in the request.

Before:

"fileName" => "contact",

 

After:

"fileName" => "contact.csv",

 

That's how I fixed it.  It literally cost of a number of hours before I find out. 

 

I hope this would help somebody out there who faced the same issue. 

 

LG

Yub

natsumimori
コミュニティーマネージャー
コミュニティーマネージャー

Getting 400 Bad Request

解決

Thank you for sharing the solution! 

0 いいね!
webdew
ガイド役 | Diamond Partner
ガイド役 | Diamond Partner

Getting 400 Bad Request

解決

Hi @YGhimire ,

400 Bad Request means your code json invalid something missing wrong in csv file.
Required fields of contacts:
firstname,lastname,email
Company :
country,name, phone


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 

0 いいね!
YGhimire
メンバー

Getting 400 Bad Request

解決

Hi @webdew ,

 

Thanks for your feedback but that didn't help.

 

Even adding lastname for contact and name for company objects, I am getting same error. 

 

It seems like something is up with the bulk import especially with api-key, I have tried to import only one object with contacts-basicApi and it works but not with the coreApi 

0 いいね!
natsumimori
コミュニティーマネージャー
コミュニティーマネージャー

Getting 400 Bad Request

解決

Thank you for your post @YGhimire .

@himanshurauthan and @MichaelC , would you be able to share your advice for @YGhimire ?

0 いいね!