Request body for Importing contacts with CSV file

SOLVED
Go to solution
WarrenL73
Rookie
0 Votes

Hello,

 

I'm looking for help with using the contact_files_import API endpoint. When sending my request I am receiving a "Unable to parse the request body as multipart form-data" error. When I manually enter the file and list_id I intend to use on the API documentation page the action is successful. However, when using my program I keep getting the error.  Below is what I'm using based on what the documentation is saying by including "list_ids" and "file" as the request body arguments. Am I missing something here?  I am using python. 

 

    email_payload= {     
        "list_ids" : ["<List ID>"],
        "file" : "file_name.csv"
    }
    
1 ACCEPTED SOLUTION
Courtney_E
Employee
0 Votes

Hello WarrenL73,

 

Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.

 

The error that you are receiving indicates that your requests are not being submitted as a multipart/form-data request body as is required when posting to the /activities/contacts_file_import  endpoint

 

Import Contacts from a CSV file

https://v3.developer.constantcontact.com/api_guide/import_contacts.html#import-contacts-from-a-csv-f...

 

I was also able to find this 3rd party resource that may be of use to you:

 

POST a Multipart-Encoded File

https://docs.python-requests.org/en/v0.8.2/user/quickstart/#post-a-multipart-encoded-file

 

Please have a look and let us know if you have any other questions! You can also email us directly at webservices.constantcontact.com


Regards,

Courtney E.
Tier II API Support Engineer

Did I answer your question?
If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.

View solution in original post

11 REPLIES 11
Courtney_E
Employee
0 Votes

Hello WarrenL73,

 

Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.

 

The error that you are receiving indicates that your requests are not being submitted as a multipart/form-data request body as is required when posting to the /activities/contacts_file_import  endpoint

 

Import Contacts from a CSV file

https://v3.developer.constantcontact.com/api_guide/import_contacts.html#import-contacts-from-a-csv-f...

 

I was also able to find this 3rd party resource that may be of use to you:

 

POST a Multipart-Encoded File

https://docs.python-requests.org/en/v0.8.2/user/quickstart/#post-a-multipart-encoded-file

 

Please have a look and let us know if you have any other questions! You can also email us directly at webservices.constantcontact.com


Regards,

Courtney E.
Tier II API Support Engineer

Did I answer your question?
If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
WarrenL73
Rookie
0 Votes

Thanks!

TylerC391
Campaign Collaborator
0 Votes

I am also having trouble submitting my request as a multipart/form-data request body (I get the "Unable to parse the request body as multipart form-data" error). I understand that I need to include the "file" and "list_ids" properties in the body, but after several attempts I still cannot figure out how to do this in C#. Here is my code:


 

 

 payload.Clear()
                    .Append("{\"file\": \"")
                    .Append(filePath)
                    .Append("\", \"list_ids\": [\"")
                    .Append(listId)
                    .Append("\"]}");

                requestUrl = "https://api.cc.email/v3//activities/contacts_file_import";

                MultipartFormDataContent form = new MultipartFormDataContent("---Boundary");
                form.Add(new StringContent(payload.ToString()), "payload");
                Stream fs = System.IO.File.OpenRead(filePath);
                string fileName = Path.GetFileName(filePath);
                form.Add(new StreamContent(fs), fileName, fileName);

                response = await httpClient.PostAsync(requestUrl, form);

 

 

Aarron_G
Employee
0 Votes

Hello @TylerC391,

Thank you for reaching out to Constant Contact API Developer Support.

I've been able to replicate the response you're receiving, and suspect that the issue is related to the request's Content-Type and/or Content-Length. I'll be conducting some further troubleshooting, and will update this thread with any findings.

Regards,


Aarron G.
API Support Engineer
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
TylerC391
Campaign Collaborator
0 Votes

Hi Aarron. Thanks for looking into this. Any updates on the troubleshooting?

Aarron_G
Employee
0 Votes

Hi Tyler,

 

You're very welcome! While I haven't yet completed troubleshooting, I should have an update for you early next week.


Aarron G.
API Support Engineer
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
TylerC391
Campaign Collaborator
0 Votes

Hi Aarron. Any updates on this?

Courtney_E
Employee
0 Votes

Hello TylerC391,

 

Thanks for your patience and sorry for the delayed response. Aaron had to assist an internal team with a project which took up most of his bandwidth for the week, but he did let me know that he plans to continue troubleshooting this issue with you as soon as he returns.

 

In the meantime, below are some third party resources that I found which may help you in narrowing down the cause of the multipart/form-data error response that you are receiving in your C# code:

 

[3rd party site] C# Corner - Upload Any File Using HTTP Post Multipart Form Data 

https://www.c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data/

 

[3rd party site] Hot Examples - C# (CSharp) HttpMultipartParser MultipartFormDataParser Examples

https://csharp.hotexamples.com/examples/HttpMultipartParser/MultipartFormDataParser/-/php-multipartf...

 

Please have a look and let us know if you have any other questions! You can also follow up with us directly via email at webservices@constantcontact.com


Regards,

Courtney E.
Tier II API Support Engineer

Did I answer your question?
If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
TylerC391
Campaign Collaborator
0 Votes

Thank you. I will look at those resources while I am waiting for Aarron to finish troubleshooting.

TylerC391
Campaign Collaborator
0 Votes

I have a colleague who can successfully complete a multipart post of a CSV file with a Python implementation. Using the following C# code, I am able to produce the same request body as he does, but I still receive the "Unable to parse the request body as multipart form-data" error:

 

using (MultipartFormDataContent form = new MultipartFormDataContent())
{
	StringContent listIdContent = new StringContent(VendorListId);
	listIdContent.Headers.Remove("Content-Type");
	form.Add(listIdContent, "\"list_ids\"");

	StringContent fileNameContent = new StringContent(fileName);
	fileNameContent.Headers.Remove("Content-Type");
	form.Add(fileNameContent, "\"file\"");

	StreamContent streamContent = new StreamContent(System.IO.File.OpenRead(filePath));
	ByteArrayContent fileContent = new ByteArrayContent(streamContent.ReadAsByteArrayAsync().Result);
	fileContent.Headers.Remove("Content-Type");
	fileContent.Headers.Add("Content-Disposition", string.Format("form-data; name=\"data\"; filename=\"{0}\"", fileName));
	form.Add(fileContent, "data", fileName);

	HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
	request.Content = form;

	response = await client.SendAsync(request);
	responseJson = response.Content.ReadAsStringAsync().Result;
}

 

 

Here is my request:

Method: POST, RequestUri: 'https://api.cc.email/v3/activities/contacts_file_import', Version: 1.1, Content: System.Net.Http.MultipartFormDataContent, Headers:
{
  Authorization: Bearer (followed by my access token)
  Content-Type: multipart/form-data; boundary="30473545-6c5e-44ff-8068-6dc192dc5241"
  Content-Length: 473
}

 

Here is my request body:

--30473545-6c5e-44ff-8068-6dc192dc5241
Content-Disposition: form-data; name="list_ids"

3b1f9b40-8e75-11ec-a8dc-fa163e5bc304
--30473545-6c5e-44ff-8068-6dc192dc5241
Content-Disposition: form-data; name="file"

example.csv
--30473545-6c5e-44ff-8068-6dc192dc5241
Content-Disposition: form-data; name="data"; filename="example.csv"

email,company_name
ddipper@example.com,Test Company
jacksbbq@example.com,Test Company

--30473545-6c5e-44ff-8068-6dc192dc5241--

 

John__B
Employee
0 Votes

Hello TylerC391,

 

We greatly appreciate your continued patience and I’m sorry for the delay in our response. Aarron has continued to assist an internal team with a project that has taken up most of his bandwidth but he does plan to continue troubleshooting this issue with you as soon as possible.

 

We appreciate the additional information you have provided regarding your colleague’s successful multipart post using a Python implementation. If you have any other questions or concerns in the meantime, please feel free to post here or reach out to us directly via email at webservices@constantcontact.com.

 

Regards,


John B.
API Support Specialist
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
Resources
Developer Portal

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

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up