Invalid URI: The Uri string is too long.

2,558 views
Skip to first unread message

Gabre71

unread,
Jul 5, 2012, 8:52:04 AM7/5/12
to rest...@googlegroups.com
I try to send a file to a website. 
In the documentation is written:
- create:
/api/s3_folders.json
POST: auth_token=TOKEN;parent_id=ID;file_name=FILENAME;file=BASE64FILE

With smaller file works all fine, but with a bit bigger file i get "Invalid URI: The Uri string is too long."



RestClient client = new RestClient(Connection.Server);

Request = String.Format("/api/s3_folders.json");

RestRequest request = new RestRequest(Request, Method.POST);

request.AddParameter("auth_token", Login.Auth_token);

request.AddParameter("parent_id", parent_id);

request.AddParameter("file_name", file_name);

// request.AddParameter("file", file);

if (file != "")

{




request.AddParameter("file", file);

}

var response = client.Execute<AppFile>(request);



Can everybody help me?

Patrick Steele

unread,
Jul 5, 2012, 2:51:43 PM7/5/12
to rest...@googlegroups.com
Are you sending the entire contents of a file? If so, that won't
really work except for very small files (due to the length of the
URI).

Instead, I think you could add the file's contents to the body of the
POST with request.AddBody(file). I've never used it before, but it
looks like that might work for you.

---
Patrick Steele
http://weblogs.asp.net/psteele

Gabre71

unread,
Jul 13, 2012, 4:38:25 AM7/13/12
to rest...@googlegroups.com
Thx Patrick!

I tried it different ways, but  isn't work....
With the first 3 parameter is no problem.
But with the 4th.... Whatever i do, on teh server side is out of sight.

tried with ParameterType.RequestBody + AddBody,
ParameterType.GetOrPost + AddFile(), etc...

No result. 

Parameter1 auth_token => srtring
Parameter2 parent_id    => integer
Parameter3 file_name    => string

Parameter4 file    => base64 encoded string
Thats all...

Please help me!!!!  :)

Patrick Steele

unread,
Jul 20, 2012, 10:39:23 AM7/20/12
to rest...@googlegroups.com
Do you have control of the server-side code? Or is this some external
software you're integrating with?

---
Patrick Steele
http://weblogs.asp.net/psteele


Rahul Bhansali

unread,
May 20, 2013, 9:34:16 AM5/20/13
to rest...@googlegroups.com
I ran into the same issue. The only difference was I had a very very large text value >100K characters instead of a file object. I was using POST method. 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