Our v3 API is now available! Learn more by visiting the v3 developer portal.

Add Library Files - Multipart POST

Use this endpoint to add files to the Library using the multipart content-type.


Click a method to view its documentation



Privileges required: mylibrary:file:create

The POST method adds a file to a Library folder using the multipart form content-type. If there are no folders in the account, set folder_id = 0.

NOTE: Set the Content-Type header to multipart/form-data.

Supported File Types

The currently supported file types you can add using POST are:

  • Image files - JPEG, JPG, GIF, PNG
  • Document files - PDF, DOC, DOCX, XLS, XLSX, PPT, PPTX

Required Multipart Elements

The multipart request needs to include the parts defined in the Structure section.

Java Example - Multipart Encoded POST

Here's an example of a multipart encoded post in Java that uploads an image file (dinnerplate-special.jpg) to a user's Library:

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Test {
       /** * @param args
        * @throws IOException
        * @throws ClientProtocolException
       public static void main(String[] args) throws ClientProtocolException, IOException {

              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost("https://api.constantcontact.com/v2/library/files");

              httppost.addHeader("Authorization", "Bearer 70e8e17d-e1ed-4b7a-8a8a-40383d74d467");
              httppost.addHeader("Accept", "application/json");
              httppost.addHeader("Content-type", "multipart/form-data");

              File fileToUse = new File("/path_to_file/YOLO.jpg"); //e.g. /temp/dinnerplate-special.jpg
              FileBody data = new FileBody(fileToUse);

              String file_type = "JPG" ;
              String description = "Oppa Gangnam Style";
              String folder_id = "-1";
              String source = "MYCOMPUTER" ;

              MultipartEntity reqEntity = new MultipartEntity();
              reqEntity.addPart("file_name", new StringBody( fileToUse.getName() ) );
              reqEntity.addPart("folder_id", new StringBody(folder_id));
              reqEntity.addPart("description", new StringBody(description));
              reqEntity.addPart("source", new StringBody(source));
              reqEntity.addPart("file_type", new StringBody(file_type));
              reqEntity.addPart("data", data);


             HttpResponse response = httpclient.execute(httppost);
             System.out.println( response ) ;

             HttpEntity resEntity = response.getEntity();
             System.out.println( resEntity ) ;
              System.out.println( EntityUtils.toString(resEntity) );


Ruby Example - Multipart Encoded POST

See an example coded in Ruby in our Github repository.

POST Status

The API performs multiple operations in order to POST a file, including performing a virus scan, so processing the POST can take a certain amount of time. You can monitor the request status using the location value in the response header.

See File Upload Status for more information.

POST: https://api.constantcontact.com/v2/library/files

Test API