HTML attachment error through API

75 views
Skip to first unread message

Alex Kalnij

unread,
Jan 24, 2012, 9:39:11 AM1/24/12
to MailChimp API Discuss
Hi.
I create a campaign through API and want to add an HTML page as the
content of the email.
I create 1(one) page with 1(one) <img> tag in it and put it into a ZIP
archive.

When I try to create campaign I get this message: "Message: The
archive was empty or otherwise corrupt. Code: 318."

When I create campaign manually through web interface and add the same
archive everything works fine.

I also tried to encode the archive in Base64. Still the same message.

Looks like a bag in the API.

Any advice?

Sincerely, Alex.

jesse

unread,
Jan 24, 2012, 9:46:53 AM1/24/12
to MailChimp API Discuss
Yes - play with it in the web app and get it working correctly there.
Then take that file and use it against the API.

Also, base64 encoding is not optional. If you need to dig in to what's
being sent across the wire, do the same test with our PHP wrapper and
compare your data with what it's sending across.

jesse

Alex Kalnij

unread,
Jan 24, 2012, 9:55:20 AM1/24/12
to MailChimp API Discuss
Thanks for quick reply.

My file uploads fine when I use web app.

Also when my HTML looks like this:
<html>
<body>
test
asdd
das
lnl
kjhn
</body>
</html>

it also works fine through API.
But if any of the strings in HTML is longer then 5-6 symbols I get the
above mentioned message.

Also if I wright the whole HTML page in one string it doesn't work.
It seems that file with long strings in it could not be parsed on your
side.

Sincerely, Alex.

jesse

unread,
Jan 24, 2012, 9:57:04 AM1/24/12
to MailChimp API Discuss
Then you likely have an encoding issue. Once based64 decoded, the API
uses the exact same code the web app does to process the file.


jesse

Alex Kalnij

unread,
Jan 24, 2012, 11:28:47 AM1/24/12
to MailChimp API Discuss
Hi again.

I represent large e-shop and we would like to cooperate with you.
Unfortunately I'm trying to get this thing work for couple of weeks
now.

I try to use your API from Java.
For encoding I use Apache's encoder and it still doesn't work.

Here is the code:

import org.apache.commons.codec.binary.Base64;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class CampaignCreateTest {

private static final String mailChimpApiUri = "https://
us4.api.mailchimp.com/1.3/?method=campaignCreate";
private static final String archiveUri = "file:/home/domosti/
temp.zip";

public static void main(String[] args) throws IOException,
URISyntaxException {

// open connection to server
URL url = new URL(mailChimpApiUri);
HttpsURLConnection conn = (HttpsURLConnection)
url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);

// prepare request

URI uri = new URI(archiveUri);
File file = new File(uri);
FileInputStream fileInputStream = new FileInputStream(file);

// translate archive file into byte array. This code was taken
from apache.commons.IOUtils
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();

byte[] buffer = new byte[1024*4];
int n = 0;
while (-1 != (n = fileInputStream.read(buffer))) {
byteArrayOutputStream.write(buffer, 0, n);
}

byte[] data = byteArrayOutputStream.toByteArray();

// I use apache's encoder
String archive = Base64.encodeBase64String(data);

String request = "{\"type\":\"regular\",\"content\":{\"archive
\":\"" + archive + "\"},\"options\":{\"subject\":\"test\",\"from_name
\":\"test\",\"from_email\":\"alex....@gmail.com\",\"to_name\":\"test
\",\"auto_footer\":false,\"list_id\":\"82ce0408ca\",\"title\":\"New\"},
\"apikey\":\"e2fb0e5f30fab6ae50275ff9f7a5ae16-us4\"}";
System.out.println(request);
OutputStream out = conn.getOutputStream();
out.write(request.getBytes());

System.out.println(conn.getResponseCode());
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream(), "UTF-8"));
String response = "";
while ((response = in.readLine()) != null)
System.out.println(response);

}

}

Substitute "archiveUri" constant with the path to you archive.

Hope you'll help.

Sincerely, Alex.

Alex Kalnij

unread,
Jan 24, 2012, 11:55:49 AM1/24/12
to MailChimp API Discuss
This code works when the file in the archive contains short strings.
When strings are long or file has an inserted image, I get the error.

Alex Kalnij

unread,
Jan 25, 2012, 6:24:10 AM1/25/12
to MailChimp API Discuss
Problem solved.
After encoding the archive to Base64 the output string should be url
encoded.
Reply all
Reply to author
Forward
0 new messages