Skip to content

Instantly share code, notes, and snippets.

@mohammedmatar
Forked from twolfson/README.md
Created April 18, 2020 03:58
Show Gist options
  • Save mohammedmatar/6454f1439afeabce88317ba8e3a83fff to your computer and use it in GitHub Desktop.
Save mohammedmatar/6454f1439afeabce88317ba8e3a83fff to your computer and use it in GitHub Desktop.
Script to upload images to Imgur via curl

With visual testing we occasionally need to debug images in CI environments. We were previously using Imgur's v1 API:

https://github.com/twolfson/twolfson.com/blob/3.102.0/test/perceptual-tests/upload-screenshots.sh

but that's been shutdown and v2 and v3 seems like they need login for uploading images. We don't like that since these are rarely setup and we don't want to associate development content with personal content.

Thankfully Imgur supports uploading images via the browser without logging in. After trial/error, we got the following script for uploading a single image:

#!/usr/bin/env bash
# Exit on first error
set -e

# Define our filepath and filename
filename="path/to/my/file.png"
content_type="image/png"
# path/to/my/file.png -> file.png
filename="$(basename filepath)"
curl -X POST "http://imgur.com/upload" \
  -H "Referer: http://imgur.com/upload" \
  -F "Filedata=@\"$filepath\";filename=$filename;type=$content_type"

# Response: {"data":{"hashes":["1YZD9eY"],"hash":"1YZD9eY","deletehash":"XA3NaYKvzQrGnCn","album":false,"edit":false,"gallery":null,"animated":false,"height":10,"width":10,"ext":".png"},"success":true,"status":200}
# Image URL: http://imgur.com/1YZD9eY

One drawback of this approach is we don't have cookies so we cannot properly delete the image =(

We could use a curl cookie jar but it's easier said than done to transfer that out of CI

Please don't abuse this setup, we don't want it to be disabled due to abuse =/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment