1

I am struggling to use the Gfytcat API to upload an mp4 from my machine. Maybe it's just me but the API docs don't seem very well fleshed out.

The following code successfully requests a new gfy, but fails the upload with the following error: 204: No Content.

using (var client = new HttpClient())
{
    var response = await client.PostAsync(@"https://api.gfycat.com/v1/gfycats", null);

    var responseString = await response.Content.ReadAsStringAsync();

    var newGfycatResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<NewGfycatResponse>(responseString);

    Console.WriteLine("gfyname: " + newGfycatResponse.gfyname);
    Console.WriteLine("secret: " + newGfycatResponse.secret);

    var filePath = @"C:\Users\Julien\Videos\black cat jumping.mp4";
    var file = File.ReadAllBytes(filePath);

    using (var content = new MultipartFormDataContent())
    {
        content.Add(new StringContent(newGfycatResponse.gfyname), "key");
        content.Add(new ByteArrayContent(file), "file", newGfycatResponse.gfyname);

        using (var message = await client.PostAsync("https://filedrop.gfycat.com", content))
        {
            var input = await message.Content.ReadAsStringAsync();

            Console.WriteLine(input);
        }
    }
}
Julien
  • 212
  • 1
  • 18
  • 53

1 Answers1

2

Looks like the below line is incorrect. You have to name the field "file", you are naming it as the name of the file.

content.Add(new StreamContent(new MemoryStream(file)), "file", newGfycatResponse.gfyname);

**** edits

You may want to modify the headers on that file content as below.

below is taken from: ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient

var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(fileName));
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "Foo.txt"
    };
    content.Add(fileContent);
Community
  • 1
  • 1
QuietSeditionist
  • 703
  • 4
  • 8
  • 18
  • thanks. this was an issue, and is now causing a different problem. question updated. – Julien Oct 17 '16 at 19:14
  • this causes Gfycat to respond with a 400 bad request, which is a step backwards i think. – Julien Oct 17 '16 at 20:06
  • Are you sure that it actually failed? 204 does not necessarily imply failure and is in fact considered a successful response in most scenarios. – QuietSeditionist Oct 17 '16 at 20:19