5

I'm pretty new to web services. Right now I have problem with using the kraken.io API to resize an uploaded image.

When requesting the response, it always throws an exception.

Any help is appreciated. Thank you very much.

Reference to kraken.io API Docs: https://kraken.io/docs/upload-url

This is what I have done so far

Trigger:

byte[] data = new byte[fuImage.PostedFile.ContentLength];
fuImage.PostedFile.InputStream.Read(data, 0, fuImage.PostedFile.ContentLength);
objKraken krakenio = new objKraken();
krakenio.wait = true;
krakenio.resize = new objKResize() { width = Base_Controller.DealsWidth, height = Base_Controller.DealsHeight, strategy = "exact" };

Controller_Kraken.UploadFile(data, krakenio);

Controller:

public const string UploadAPIUrl = "https://api.kraken.io/v1/upload";

public static bool UploadFile(byte[] data, objKraken krakenInfo)
{
    try
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UploadAPIUrl);
        webRequest.Method = "POST";
        webRequest.ContentType = "multipart/form-data";
        string jsonString = JsonConvert.SerializeObject(krakenInfo);
        webRequest.ContentLength = data.Length + jsonString.Length;

        using (Stream postStream = webRequest.GetRequestStream())
        { // Send the data. 
            postStream.Write(data, 0, data.Length);
            using (StreamWriter swRequest = new StreamWriter(postStream))
            {                        
                swRequest.Write(jsonString);
                swRequest.Flush();
            }
            postStream.Close();
        }

        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
            {
                string responseText;
                using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    responseText = reader.ReadToEnd();
                }
                if (responseText == "true")
                { return true; }
                else
                { return false; }
            }
            else
            { return false; }
        }
    }
    catch
    { return false; }
}

Object:

public class objKraken
{
    public objKAuth auth { get { return new objKAuth(); } }
    public objKResize resize { get; set; }
    public bool wait { get; set; }
}

public class objKAuth
{
    public string api_key { get { return ConfigurationManager.AppSettings["ApiKey"]; } }
    public string api_secret { get { return ConfigurationManager.AppSettings["SecretKey"]; } }
}

public class objKResize
{
    public int width { get; set; }
    public int height { get; set; }
    public string strategy { get; set; }
}
CSDev
  • 3,177
  • 6
  • 19
  • 37
kml
  • 163
  • 6
  • **What does the error say**? – SLaks May 04 '14 at 16:00
  • 400 Bad Request. I'm pretty sure that my request is wrong, because if I use the API for url, which is only send json text, it works perfectly. If I see in the API Docs, it says "The request will consist of text (JSON) and binary data (image file) which makes it a Multipart Request.", that's the part that I don't know. – kml May 04 '14 at 16:03
  • 1
    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 – SLaks May 04 '14 at 16:56
  • I'm having the same problem, what did you change in the end to make it work? @kml – Nhím Hổ Báo Jul 07 '14 at 17:20
  • 2
    @NhímHổBáo, I'm using RestSharp Library to make it work http://restsharp.org/ and here are the following reference to use it: https://github.com/timw255/SitefinityKrakenIO and https://github.com/timw255/SitefinityKrakenIO/tree/master/Custom/AlbumOptimization – kml Jul 10 '14 at 18:53

0 Answers0