URI too long error on POST (will not place data in body)

80 views
Skip to first unread message

Derrick Sullivan

unread,
Apr 23, 2013, 11:57:24 PM4/23/13
to rest...@googlegroups.com
I am attempting to load a large amount of text with a parameter.  The server is responding that it does not see the label. When I post it using standard addparameter, it works with small test strings but with my large block of text I get a URI too long.

What I am reading tells me that this large block should travel in the body, not the URI.

Am I missing something?

Patrick Steele

unread,
Apr 24, 2013, 7:19:25 AM4/24/13
to rest...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "RestSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restsharp+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rahul Bhansali

unread,
May 20, 2013, 9:31:39 AM5/20/13
to rest...@googlegroups.com
I ran into the same issue. The message Uri is too long completely threw me off since I wasn't expecting my data to travel in the URL at all. After much research I found a resolution to my problem.
Here's what I did. 
- Downloaded the source code from GitHub. 
- Began debugging to identify the point of failure. I traced it down to a part which tries to HTML encode the text using the method Uri.EscapeDataString() 
- The problem is it can only process a string with max length of 65519 characters. See http://stackoverflow.com/questions/6695208/uri-escapedatastring-invalid-uri-the-uri-string-is-too-long
- So in order to resolve this issue: I replaced the default UrlEncode() extension method which used to look like this: 
public static string UrlEncode(this string input)
{ return Uri.EscapeDataString(input); }
to 

public static string UrlEncode(this string input)
{
            int limit = 65519;

            StringBuilder sb = new StringBuilder();
            int loops = input.Length / limit;

            for (int i = 0; i <= loops; i++)
            {
                if (i < loops)
                {
                    sb.Append(Uri.EscapeDataString(input.Substring(limit * i, limit)));
                }
                else
                {
                    sb.Append(Uri.EscapeDataString(input.Substring(limit * i)));
                }
            }

            return sb.ToString();
}
- Rebuilt the solution and copied the new dlls
- Problem resolved :) 
Reply all
Reply to author
Forward
0 new messages