APIs & Integrations

YGhimire
Membre

Getting 400 Bad Request

Résolue

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 Votes
1 Solution acceptée
YGhimire
Solution
Membre

Getting 400 Bad Request

Résolue

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

Voir la solution dans l'envoi d'origine

6 Réponses
jtribble
Participant

Getting 400 Bad Request

Résolue

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 Votes
YGhimire
Solution
Membre

Getting 400 Bad Request

Résolue

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
Gestionnaire de communauté
Gestionnaire de communauté

Getting 400 Bad Request

Résolue

Thank you for sharing the solution! 

0 Votes
webdew
Guide | Partenaire solutions Diamond
Guide | Partenaire solutions Diamond

Getting 400 Bad Request

Résolue

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 Votes
YGhimire
Membre

Getting 400 Bad Request

Résolue

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 Votes
natsumimori
Gestionnaire de communauté
Gestionnaire de communauté

Getting 400 Bad Request

Résolue

Thank you for your post @YGhimire .

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

0 Votes