Search code examples
javascriptajaxapiimage-uploadingimgur

Sideloading images to imgur without API key


I've been trying to use the feature of the imgur API that allows you to just send a GET request to http://api.imgur.com/2/upload with a URL in the form data, and have that image uploaded, but I can't get it to work, it just returns nothing.

$.get("http://api.imgur.com/2/upload.json", {
  url: 'http://upload.wikimedia.org/wikipedia/commons/3/3e/Phalaenopsis_JPEG.png'
}, function(data) {
  return console.log(data);
});

Are there any alternatives? Or does anyone know how I can get the above code to work?

imgur API documentation here


Solution

  • Ah, it was in fact working!

    The location of the uploaded image was being returned as Location in the response headers.

    Edit:

    I found that I wasn't able to access the headers so I had to come up with something else. Here's a snippet to upload using YQL:

    urlToImgur = (url, callback) ->
      upload_url = "http://api.imgur.com/2/upload?url=#{url}"
      $.ajax
        url: 'http://query.yahooapis.com/v1/public/yql'
        dataType: 'jsonp'
        data:
          q: "select none from html where url='#{upload_url}'"
          diagnostics: true
        success: (data) ->
          redirects = data.query.diagnostics.redirect
          image_url = redirects[redirects.length-1].content
          callback image_url