Uploading .mp4 via HTTPS

171 Views Asked by At

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;

        }
    }
2

There are 2 best solutions below

5
On

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.

0
On

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");