Add Library Files - Multipart POST

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

Methods:

Click a method to view its documentation

POST

DescriptionTOP

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);

              httppost.setEntity(reqEntity);

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

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

              EntityUtils.consume(resEntity);
              httpclient.getConnectionManager().shutdown();
      }
}

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

name

type

default

description

api_key

query

REQUIRED; The API key for the application

Response CodesTOP

code

description

202

Library file was successfully created

400

Either JSON was malformed or there was a data validation error

401

Authentication failure

500

Internal server error occurred

StructureTOP

property

type(max length)

description

data

string

REQUIRED. Part of the multipart requestbody; it's the data contained in the file being uploaded

description

string (100)

REQUIRED. Part of the multipart requestbody; it is the description of the user-provided file being uploaded

file_name

string (80)

REQUIRED. Part of the multipart requestbody; it is the filename and extension

file_type

string

REQUIRED. Part of the multipart requestbody; specifies the file type, valid values are: JPEG, JPG, GIF, PDF, PNG

folder_id

string

REQUIRED. Part of the multipart requestbody, the id of the folder you are putting the file in. Use 0 if there are no folders in the account.

source

string

REQUIRED. Part of the multipart requestbody; indicates the source of the original file; image files can be uploaded from the following sources

  • MyComputer
  • Facebook - MyLibrary Plus customers only
  • Instagram - MyLibrary Plus customers only
  • Shutterstock
  • Mobile