0

I'm trying to upload an .mp4 file to Giphy.com's API. It says to send the file over as 'Binary' and I think I'm confused as what exactly they mean by that. Here's the docs if you scroll to the bottom at "Upload Endpoint". https://developers.giphy.com/docs/

Here's what I have right now.

I've tried multiple versions of this (using StringContent, MultipartFormDataContent, ByteArrayContent, HttpMessages... etc) and always get a '400 - Bad Request - No Source Url' (which the docs say isn't required if you upload you're own) which makes me believe the content isn't being recognized.

    public async Task<HttpResponseMessage> UploadVideoAsync(StorageFile file)
    {
        using (var stream = await file.OpenStreamForReadAsync())
        {
            byte[] bytes = new byte[stream.Length];
            await stream.ReadAsync(bytes, 0, (int)stream.Length);

            Dictionary<string, string> dic = new Dictionary<string, string>
            {
                { "file", Encoding.ASCII.GetString(bytes) },
                { "api_key", api_key }
            };

            MultipartFormDataContent multipartContent = new MultipartFormDataContent();
            multipartContent.Add(new ByteArrayContent(bytes));
            var response = await httpClient.PostAsync($"v1/gifs?api_key={api_key}", multipartContent);
            var stringResponse = await response.Content.ReadAsStringAsync();
            return response;

        }
    }
aweyeahdawg
  • 886
  • 9
  • 19

2 Answers2

0

It seems that your code doesn't match {api_key} properly. You don't use the "dic" variable anywhere. You can try with v1/gifs?api_key=YOUR_API_KEY&file= instead. Where YOUR_API_KEY should be replaced by your API key obtained from giphy.

VuVirt
  • 1,887
  • 11
  • 13
  • Sorry, I was using that before but then changed to using that ByteArrayContent and the api_key in the url. The problem with adding the file to the url is that it is 1.4 mill characters long. Uris can only be around 2000. – aweyeahdawg Aug 15 '18 at 16:25
  • Do you get any response (and what) if you send only this: v1/gifs?api_key=YOUR_API_KEY – VuVirt Aug 15 '18 at 17:08
  • Or do it directly like this: ByteArrayContent byteContent = new ByteArrayContent(data); HttpResponseMessage reponse = await client.PostAsync(uri, byteContent); – VuVirt Aug 15 '18 at 17:10
  • I get the 'No Source Url' Error. That's why I think it's the formatting of the content because it's the same error either way. I'll try adding it directly when I get home. – aweyeahdawg Aug 15 '18 at 17:12
  • Are you sure that you need to put the actual file byte array? Can you try to put a local filename instead after file= – VuVirt Aug 15 '18 at 18:36
0

always get a '400 - Bad Request - No Source Url' (which the docs say isn't required if you upload you're own) which makes me believe the content isn't being recognized.

You need to apply a name for the ByteArrayContent. The document has shown that Request Parameters contains 'file: string (binary) required if no source_image_url supplied'.

The code should like the following:

MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(bytes),"file");
Xie Steven
  • 8,544
  • 1
  • 9
  • 23