HTTP POST IMPORT Failing using C#

Hello everyone,
I’m trying to implement HTTP POST to import CSV data but always getting errors. i tried to follow similar subjects here but no success.
Here is the ERROR

{"status":"error","message":"Unable to process JSON","correlationId":"06cadc87-9dc7-4627-8c25-fe5029c27180"}

C# CODE

 private readonly string filePath = @"..\net8.0\Data\hubspot_sample.csv";
 private readonly string requestJsonPath = @"..\net8.0\Data\request.json";

 public async Task UploadCSV()
 {

 var importRequest = System.IO.File.ReadAllText(requestJsonPath);

 var formContent = new MultipartFormDataContent();
 formContent.Add(
 new StringContent(
 importRequest,
 System.Text.Encoding.UTF8,
 "application/json"),
 "importRequest");

 // importFiles is a string[] of full file paths

 formContent.Add(
 new StreamContent(System.IO.File.OpenRead(filePath)),
 "files",
 Path.GetFileName(filePath));

 var requestMessage = new HttpRequestMessage(
 HttpMethod.Post,
 "https://api.hubapi.com/crm/v3/imports"
 )
 {
 Content = formContent
 };

 requestMessage.Headers.Add("Authorization", $"Bearer {API_KEY}");

 requestMessage.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

 var response = await new HttpClient().SendAsync(requestMessage);
 if (response.Content != null)
 {
 var jsonResponse = await response.Content.ReadAsStringAsync();

 }
 }

Request.json

// Example POST to https://api.hubspot.com/crm/v3/imports
// Content-Type header set to multipart/form-data

{
 "name": "Association Label Import",
 "dateFormat": "DAY_MONTH_YEAR",
 "files": [
 {
 "fileName": "hubspot_sample.csv",
 "fileFormat": "CSV",
 "fileImportPage": {
 "hasHeader": true,
 "columnMappings": [
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Email",
 "propertyName": "email"
 },
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Domain",
 "propertyName": "domain"
 }
 //,
 //{
 // "columnName": "Association Label",
 // "columnType": "FLEXIBLE_ASSOCIATION_LABEL",
 // "columnObjectTypeId": "0-1",
 // "toColumnObjectTypeId": "0-2"
 //}
 ]
 }
 }
 ]
}

hubspot_sample.csv

Email,Domain
test2@test.com,www.test2.com,

Can anyone help what’s wrong ? Thanks

@SImadEddine - can I ask in what execution environment are you calling these code fragments? My question is related to whether it is server side (within HubSpot) or client side.

Steve

Hi Steve,
It is client side, just testing console app in my visual studio at the moment

Its hard for me to follow all the flow in your code framents - but something that has always given me problems with this API is the header of the request. It should NOT contain application/json I believe.

Good luck!

Steve

Hi @SImadEddine :waving_hand:

Have you tried removing the commented-out lines in your Request.json file? I’ve found some HubSpot API endpoints respond with errors when there are comments included in JSON. Hopefully it’s as simple as that :crossed_fingers:

Please let me know if you have any follow-up questions.

Hi Zach, that’s true thank you