Skip to content

Instantly share code, notes, and snippets.

@michaelmiller2116
Last active April 18, 2018 21:25
Show Gist options
  • Save michaelmiller2116/83c6d84c5893fb3a22108518deb63245 to your computer and use it in GitHub Desktop.
Save michaelmiller2116/83c6d84c5893fb3a22108518deb63245 to your computer and use it in GitHub Desktop.
API templates

Fetch Url Then Parse Then Handle Catch

Example:

GET::::::::::::::::::::::::::::::::::::::::::::::::::

fetch("http://jservice.io/api/clues")
    .then(function(response) {
        return response.json()
    })
    .then(function(data) {
        console.log(response)
    })
    .catch(function(error) {
        console.log("error", error.message)
    })

POST:::::::::::::::::::::::::::::::::::::::::::::::::::

const data = new FormData ({
    key: "value",
    gschool: "rocks",
    fetch: "rocks"
})

fetch('https://g-echo-request.herokuapp.com/1evt5az1', {
    method: "post",
    body: data
})
  .then(function(response) {
    return response.text();
  })
  .then(function(data) {
      console.log(data);
  })
  .catch(function(error) {
    console.log('error:', error.message);
  });
  
  AND
  
  fetch('https://galvanize-leader-board.herokuapp.com/api/v1/leader-board', {
            method: "POST",
            headers: new Headers({
                "content-type": "application/json"
            }),
            body: JSON.stringify({
                game_name: "GBP",
                player_name: `${nameValue}`,
                score: `${score}`
            })
        })
        .then(response => response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment