Issue with RestSharp and status code of 0

8,266 views
Skip to first unread message

Joel Christner

unread,
Aug 27, 2012, 11:31:32 PM8/27/12
to rest...@googlegroups.com
Hi all,

I'm using RestSharp with Mailgun (great service, by the way!) and I'm encountering something strange.  RestSharp works like a champ when sending small messages, but when I try to send something >100KB in size, RestClient.Execute returns a status code of 0 and I see nothing in my web proxy (using Charles Proxy).  

When I use the exact same code, but with a smaller message, it works just fine.  

If you're not familiar with Mailgun, everything is added to the POST request payload using RestRequest.AddParameter.

I apologize if I have failed to find public documentation that might give me a better idea of how to troubleshoot this.  Any help would be greatly appreciated.

Thanks,
Joel

Joel Christner

unread,
Aug 27, 2012, 11:38:21 PM8/27/12
to rest...@googlegroups.com
In case anyone is interested in the code I'm using...  (reduced)

        public bool i_sendemail_mailgun(email_object email_obj)
        {
            try
            {
                
// some setup code removed from here.  The variables should be self-explanatory.

                RestClient client = new RestClient();

                client.BaseUrl = mailgun_url;
                client.Authenticator = new HttpBasicAuthenticator("api", mailgun_api_key);

                RestRequest request = new RestRequest();
                request.AddParameter("domain", mailgun_domain, ParameterType.UrlSegment);
                request.Resource = mailgun_domain + "/" + mailgun_resource_send_message;
                request.AddParameter("from", email_obj.from_addr);
                request.AddParameter("to", email_obj.to_addr);
                request.AddParameter("subject", email_obj.email_subj);
                request.AddParameter("cc", email_obj.cc_addr);
                request.AddParameter("bcc", email_obj.bcc_addr);

                if (email_obj.is_html)
                {
                    request.AddParameter("html", email_obj.email_body);
                }
                else
                {
                    request.AddParameter("text", email_obj.email_body);
                }

                request.Method = Method.POST;

                IRestResponse resp = client.Execute(request);
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    // I get this when sending the small message...
                    i_syslog_debug("i_sendemail_mailgun message " + email_obj.guid + " sent successfully");
                    return true;
                }

                // and I get this when sending the large message...
                i_syslog_debug("i_sendemail_mailgun message " + email_obj.guid + " not sent successfully: " + resp.StatusCode + " " + resp.StatusDescription);
                return false;
            }
            catch (Exception e_outer)
            {
                // never gets here...
                i_exception("i_sendemail_mailgun", "exception encountered when attempting to send email", e_outer); 
                return false;

Joel Christner

unread,
Aug 29, 2012, 3:06:59 PM8/29/12
to rest...@googlegroups.com
Definitely a RestSharp problem.  I rolled my own REST client and it didn't have an issue.

Not sure if it's worth investigating or not, because now I don't need it fixed.  

Joe Berni

unread,
Sep 20, 2013, 4:35:50 AM9/20/13
to rest...@googlegroups.com, joel.ch...@gmail.com
Joel - I'm running into this problem as well. Can you please post what you changed in your fork of RestSharp?
 
Thank you.

Joel Christner

unread,
Sep 20, 2013, 11:20:05 AM9/20/13
to rest...@googlegroups.com
Hi Joe, I didn't fork RestSharp.  I built my own using HttpWebRequest.

Joe Berni

unread,
Sep 20, 2013, 9:39:59 PM9/20/13
to rest...@googlegroups.com
Ugh.  That's the complete opposite of what I was hoping to hear :-).  Thank you for the response Joel.
 
Joe

Joe Berni

unread,
Sep 21, 2013, 11:05:22 PM9/21/13
to rest...@googlegroups.com
I found the solution in case anyone else comes across this same issue.  For clarification, I was trying to create a new template in MailChimp via their API.  When trying to create my new template (using the RestSharp library) I was creating a REST request and using the following method:
request.addparameter("html", myNewTemplateSourceCode)
Using this method the creation of any template that was over approx 65kb in size would fail (no error response - just no template created in MC). According to the RestSharp documentation creating POST parameters with the above format adds the parameters to the request body (i.e. it does not append them as query string parameters). However, there must be something in the library that choked when a parameter was larger than 65kb.
To solve the issue I created an object for my new template with the properties apikey, name and html. I then used the following RestSharp method:
request.addbody(myMailChimpTemplateObject)
Using this method I was able to create a template via the API that was over 65kb. 
Reply all
Reply to author
Forward
0 new messages