2

The below code is used to send emails through the pardot api.

if (ConfigurationManager.AppSettings.Count > 0)
        {
            uri = ConfigurationManager.AppSettings["PardotURI"].ToString() + "email/version/4/do/send/prospect_email/" + email;
            uri += "?user_key=" + ConfigurationManager.AppSettings["PardotUserKey"].ToString();
            uri += "&api_key=" + GetAPIKey() + "&campaign_id=" + GetPardotCampaign("Capis News");
            uri += "&from_email=" + ConfigurationManager.AppSettings["FromEmail"].ToString();
            uri += "&from_name=" + ConfigurationManager.AppSettings["FromName"].ToString();
            uri += "&name=FlyNews - " + DateTime.Now.ToString("MM/dd/yyy h:mm tt");
            uri += "&subject=CAPIS: Client Holdings News " + DateTime.Today.ToString("MM/dd/yyyy");
        }

        try
        {
            MultipartFormDataContent data = new MultipartFormDataContent();

            data.Add(new StringContent(htmlContent), "html_content");
            data.Add(new StringContent(textContent), "text_content");

            await client.PostAsync(uri, data);
            client.Dispose();
        }
        catch(Exception ex)

It was working great until I noticed a few days ago it started throwing the following exception. Unfortunately it isn't consistent because it will send 30/40 emails but throw exceptions for the other 10, and the number of exceptions each day has been different and for different people. I know the data im sending in the multipartform is large but that shouldn't be part of the uri and unless someone has a 1500 character email the uri should never be too long. Does anyone have any idea of what may be going on? I appreciate any help.

System.UriFormatException: Invalid URI: The Uri string is too long. at System.UriHelper.EscapeString(String input, Int32 start, Int32 end, Char[] dest, Int32& destPos, Boolean isUriString, Char force1, Char force2, Char rsvd) at System.Uri.EscapeDataString(String stringToEscape) at System.Net.Http.FormUrlEncodedContent.Encode(String data) at System.Net.Http.FormUrlEncodedContent.GetContentByteArray(IEnumerable1 nameValueCollection) at System.Net.Http.FormUrlEncodedContent..ctor(IEnumerable1 nameValueCollection) at PardotDataAccessLibrary.PardotDataAccess.d__9.MoveNext()

Dan Def
  • 1,836
  • 2
  • 21
  • 39

3 Answers3

5

This is a known issue in pretty much all flavors of .NET. Even though the exception message says "Invalid URI", you'll notice from the stack trace that it's being thrown from FormUrlEncodedContent. So the body of the request is the problem.

One way to get around this is to use Flurl (disclaimer: I'm the author) to make the request. I've explicitly fixed this issue in Flurl's implementation. And as a bonus it will clean up your URL-building and content-building code significantly:

await ConfigurationManager.AppSettings["PardotURI"]
    .AppendPathSegments("email/version/4/do/send/prospect_email", email)
    .SetQueryParams(new {
        user_key = ConfigurationManager.AppSettings["PardotUserKey"],
        pi_key = GetAPIKey() + "&campaign_id=" + GetPardotCampaign("Capis News");
        from_email = ConfigurationManager.AppSettings["FromEmail"],
        from_name = ConfigurationManager.AppSettings["FromName"],
        name = "FlyNews - " + DateTime.Now.ToString("MM/dd/yyy h:mm tt"),
        subject = "CAPIS: Client Holdings News " + DateTime.Today.ToString("MM/dd/yyyy")
    })
    .PostUrlEncodedAsync(new {
        html_content = htmlContent,
        text_content = textContent
    });
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
-1

I had similar issue while using the "MultipartFormDataContent"and I wouldn't recommand to use it.

Now, I use JSON for that.

You might find more info right here : https://stackoverflow.com/a/38440832/6488079

Hope it helps !

Phteven
  • 47
  • 6
  • The OP is calling a 3rd party API. He can't simply use JSON, he needs to use what the API supports. – Todd Menier Jan 18 '19 at 13:20
  • Actually, you're right. The "pardot api" seems to only accept query parameters and I might have kept in mind my own exprience. Still the link I provided can help for this issue, especially that comment : https://stackoverflow.com/a/48393926/6488079 – Phteven Jan 18 '19 at 13:31
-1

Sorry, I found a fix for me the day after posting this and moved on forgetting about the post. I changed the api calls to synchronous and haven't seen an error since. I think because the api i'm having to use limits you to 5 concurrent calls maybe having them asynchronous caused an access amount of calls and therefor errors.

  • The exception details you posted in the question clearly indicate that the error occurs on serializing the request body, which happens before the request is even sent. So it would have nothing to do with rate limits or sync vs async. If you're not seeing errors anymore, it's sheer coincidence - you either haven't created a request body that's over 65,520 characters or you switched to a (synchronous) library that doesn't use `Uri.EscapeDataString`. – Todd Menier Feb 02 '19 at 02:53