0

I am using imgur API to upload images but i am getting an exception on this line:

string uploadRequestString = "image=" + Uri.EscapeDataString(Convert.ToBase64String(imageData)) + "&key=" + apiKey;

Invalid URI: The Uri string is too long.

Full code:

public static string PostToImgur(string imagFilePath, string apiKey)
{
    byte[] imageData;
    FileStream fileStream = File.OpenRead(imagFilePath);
    imageData = new byte[fileStream.Length];
    fileStream.Read(imageData, 0, imageData.Length);
    fileStream.Close();

    string uploadRequestString = "image=" + Uri.EscapeDataString(Convert.ToBase64String(imageData)) + "&key=" + apiKey;

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://api.imgur.com/2/upload");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ServicePoint.Expect100Continue = false;

    StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
    streamWriter.Write(uploadRequestString);
    streamWriter.Close();

    WebResponse response = webRequest.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);

    string responseString = responseReader.ReadToEnd();

    XmlDocument doc = new XmlDocument();
    doc.InnerXml = responseString;
    XmlElement root = doc.DocumentElement;
    responseString = root.GetElementsByTagName("original")[0].InnerText;

    return responseString;
}

It works for smaller size file but getting that error on large files.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user969068
  • 2,818
  • 5
  • 33
  • 64

4 Answers4

2

I suspect that the output from System.Convert.ToBase64String(imageData) is too long to be a valid URI which is around 2000 characters (I think it's 2048).

This will be related to the size of the image as a smaller image can be encoded into a shorter string.

You aren't going to be able to get round this limit.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Thanks for your reply sir, If i upload a 20Kb it works but i tried on 500Kb sized image and i see that error. – user969068 Feb 13 '12 at 09:26
2

Use a shorter URL. The URLL per specification is limited and uploading binary data WILL blow this length.

Noone does it - normally data is attached as a form request variables, no part of the URL.

"image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData))

WILL NOT WORK. You can not have multi megaby URL's.

What is the maximum length of a URL in different browsers?

has a discussion. Conclusion is that about 2000 characters is maximum length.

Anyhow, put the image into a variable that is part of the request payload, not the url.

Community
  • 1
  • 1
TomTom
  • 61,059
  • 10
  • 88
  • 148
0

Easy with http://restsharp.org/

FileStream fileStream = File.OpenRead(imagFilePath);
imageData = new byte[fileStream.Length];
fileStream.Read(imageData, 0, imageData.Length);
fileStream.Close();

RestClient client = new RestClient("https://api.imgur.com/3/image");
        RestRequest request = new RestRequest()
        {
            Method = Method.POST,
        };
request.AddHeader("Authorization", "YOUR Client-ID");
request.AddParameter("image", Convert.ToBase64String(imageData), ParameterType.RequestBody);
IRestResponse restResponse = client.Execute(request);
Erik Bergstedt
  • 912
  • 10
  • 27
0

You can use a PUT request if possible.

STM
  • 954
  • 6
  • 16