Fetch API cheatsheet

Download printable version: Fetch API cheatsheet (PDF, 4.2MB), processing results cheatsheet (PDF, 3MB)

GETPOST
fetch('{url}')
    .then(response => console.log(response));
fetch('{url}', {
    method: 'post'
})
    .then(response => console.log(response));
simple
fetch('{url}', {
    headers: {
        'Authorization': 'Basic {token}'
    }
})
    .then(response => console.log(response));
fetch('{url}', {
    method: 'post',
    headers: {
        'Authorization': 'Basic {token}'
    }
})
    .then(response => console.log(response));
authorization token (Bearer)
fetch('{url}?var1=value1&var2=value2')
	.then(response => console.log(response));
fetch('{url}?var1=value1&var2=value2', {
    method: 'post'
})
    .then(response => console.log(response));
querystring data
fetch('{url}', {
    mode: 'cors'
})
    .then(response => console.log(response));
fetch('{url}', {
    mode: 'cors',
	method: 'post'
})
    .then(response => console.log(response));
CORS
fetch('{url}?var1=value1&var2=value2', {
    headers: {
        'Authorization': 'Bearer {token}'
    }
})
    .then(response => console.log(response));
fetch('{url}?var1=value1&var2=value2', {
    method: 'post',
    headers: {
        'Authorization': 'Bearer {token}'
    }
})
    .then(response => console.log(response));
authorization token + querystring data
let formData = new FormData();
formData.append('field1', 'value1');
formData.append('field2', 'value2');

fetch('{url}', {
    method: 'post',
    body: formData
})
    .then(response => console.log(response));
form data
fetch('{url}', {
    method: 'post',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        'field1': 'value1',
        'field2': 'value2'
    })
})
    .then(response => console.log(response));
JSON data
fetch('{url}', {
    method: 'post',
    mode: 'cors',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        'field1': 'value1',
        'field2': 'value2'
    })
})
    .then(response => console.log(response));
JSON data + CORS

Processing response

PromiseAsync/await
fetch(...)
	.then(response => {
		if (response.status == 200){
			// all OK
		} else {
			console.log(response.statusText);
		}
	});
async function getData(){
	let response = await fetch(...);
	
	if (response.status == 200){
		// all OK
	} else {
		console.log(response.statusText);
	}
}
checking status code
var userId;

fetch(...)
    .then(response => response.text())
    .then(id => {
        userId = id;
        console.log(userId)
    });
async function getData(){
	let response = await fetch(...);
	let userId = await response.text();
	
	console.log(userId);
}
getting a simple value
var dataObj;

fetch(...)
    .then(response => response.json())
    .then(data => {
        dataObj = data;
        console.log(dataObj)
    });
async function getData(){
	let response = await fetch(...);
	let dataObj = await response.json();
	
	console.log(dataObj);
}
getting JSON data