Forked from prof3ssorSt3v3/fetch-request-headers.js
Created
November 22, 2020 14:12
-
-
Save lisawebcoder/8b170c5f9356e412ae943e7f2abcf7e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//fetch using a Request and a Headers objects | |
//using jsonplaceholder for the data | |
const uri = 'http://jsonplaceholder.typicode.com/users'; | |
//new Request(uri) | |
//new Request(uri, options) | |
//options - method, headers, body, mode | |
//methods: GET, POST, PUT, DELETE, OPTIONS | |
//new Headers() | |
// headers.append(name, value) | |
// Content-Type, Content-Length, Accept, Accept-Language, | |
// X-Requested-With, User-Agent | |
let h = new Headers(); | |
h.append('Accept', 'application/json'); | |
let req = new Request(uri, { | |
method: 'POST', | |
headers: h, | |
mode: 'cors' | |
}); | |
fetch(req) | |
.then( (response)=>{ | |
if(response.ok){ | |
return response.json(); | |
}else{ | |
throw new Error('BAD HTTP stuff'); | |
} | |
}) | |
.then( (jsonData) =>{ | |
console.log(jsonData); | |
}) | |
.catch( (err) =>{ | |
console.log('ERROR:', err.message); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment