0

I have a problem with using Imgur API. I received clientId for anon uploading of media files. Then I tried to use this credentials for API requests. I had problems with using 'https://api.imgur.com/3/upload' route for uploading from the client browser Javascript, but then I found that 'https://api.imgur.com/3/image' works as expected with images uploading (in base64 format), but not working as expected with video uploading (in binary format, because of base64 returns error), after changing method of sending video in binary format doing few requests I managed to receive 200 response code, but the only info I received (besides status info) was 'ticket' field, and nothing else (no error message or code). And I don't know how to use this info to get access to the uploaded video. This type of response not documented anywhere and I'm not really sure it should be working like it is now. Can you please help me with this case? Maybe someone had a similar situation. I have attached 'har' request info from Chrome so that you could see how I made a request, maybe I did mistake somewhere. Really waiting for your answer, thanks!

HAR request info

Send file function:

 sendFile = async (fileObj) => {
    const myHeaders = new Headers();
    myHeaders.append('Authorization', uploadCredentials);

    const formData = new FormData(),
        {
            type
        } = fileObj,
        fileTitle = fileObj.file.name.split('.').shift(),
        splitDataMarker = ';base64,';
    let fileData;

    if (type === 'image') {
        const indexOfMarker = fileObj.data.indexOf(splitDataMarker),
            indexOfDataStart = indexOfMarker ? indexOfMarker + splitDataMarker.length : 0;

        fileData = fileObj.data.substr(indexOfDataStart);
        formData.append('type', 'base64');
    } else if (type === 'video') {
        formData.append('type', 'file');
        const res = await fetch(fileObj.data);
        fileData = await res.blob();
    }
    formData.append(type, fileData);
    formData.append('name', fileObj.file.name);
    formData.append('title', fileTitle);

    const requestOptions: any = {
        method: 'POST',
        headers: myHeaders,
        body: formData,
        redirect: 'follow',
    };

    try {
        const res = await fetch(uploadUrl, requestOptions),
            data = await res.json();

        return data;
    } catch (error) {
        throw Error(error);
    }
},
Raghul SK
  • 1,256
  • 5
  • 22
  • 30

1 Answers1

1

Your question is answered here: https://stackoverflow.com/a/57240243

In short: for videos you need to use the /upload api endpoint and include a file with a name of video.

If you are including the file as a base64 encoded value in the POST body then you need to set the type to base64, if it is attached, then you need to set the type to file:

# If base64 body data:
curl --location --request POST 'https://api.imgur.com/3/upload' --header 'Authorization: Client-ID 3a49f81624b9a16' --data 'image=R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&type=base64&disable_audio=0'
CLIENT_ID=3a49f81624b9a16; curl --location --request POST 'https://api.imgur.com/3/upload' --header "Authorization: Client-ID $CLIENT_ID" --data 'image=R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&type=base64&disable_audio=0'


# If reading file (in this case, from stdin:
echo 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' | base64 --decode | curl --location --request POST 'https://api.imgur.com/3/upload' --header "Authorization: Client-ID $CLIENT_ID" -F 'type=file' -F 'disable_audio=0' -F 'image=@-'

In both examples above, for a video, change image= to video=

Oliver I
  • 436
  • 1
  • 3
  • 12