Fetches a resource from the network.
fetch(request, config?)
request
A string containing the URL to fetch.
(Currently, URLRequest as request parameter is not supported.)
config optional
An object containing the configuration options:
method optional
The HTTP method to use, such as "GET", "POST", "PUT", "DELETE", etc.
If omitted, defaults to "GET".
headers optional
An object containing the HTTP headers to send with the request.
Each property name is the name of a header, and the value is the value of the header.
If omitted, defaults to an empty object.
body optional
The body of the request, which can be a string, Blob or FormData.
A promise that resolves to a Response object.
Rejects a promise on network error. The server response is not considered an error: the promise resolves with a Response object even if the server returns an HTTP error status code.
fetch('https://www.rfc-editor.org/rfc/rfc2616.txt')
.then(r => r.text())
.then(s => print(x.split('\n')[23].trim()))
Hypertext Transfer Protocol -- HTTP/1.1
let params = new URLSearchParams({ q: "stars:>1000", sort: "stars" });
let response = await fetch(`https://api.github.com/search/repositories?${params}`)
let results = await response.json()
results.items.slice(0, 4).map(item => item.full_name)
["freeCodeCamp/freeCodeCamp", "EbookFoundation/free-programming-books", "996icu/996.ICU"]
fetch('https://example.com', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ foo: 'bar' })
})