Upload files as a gist using JavaScript's fetch API

I've been using fetch for most of my AJAX calls lately. There has been quite a bit of library fragmentation within the JS community with AJAX. Nobody wants to bother with the clunky mess that the original API is and the old gold standard of jQuery continues to be used less and less. I like the interface for superagent the most. Fetch provides a decent one that is quickly becoming standardized among all of the browsers and node. The polyfill for it is surprisingly complete.

I wrote a script as a quick way to get files up to a public endpoint, and it turned out to be a great usecase for fetch.

fetch('https://api.github.com/gists', {  
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        public: true,
        files: {
            [file.name]: {
              content: content
          }
        },
      })
    })

This will get you an anonymous gist with a single file. The github API is intutive and easy to use. It would be no problem to extent it to do multiple files, and seems to be very generous as far as rate limiting and other restrictions.

To see it as part of a more elaborate file upload process, check out the jsfiddle below:

Bret Lowrey

Code is like a war - the best code is one never written.

Florida, USA