Skip to main content
Version: v3.x

Fetch API

The Fetch API can be used to send HTTP requests in JS. The use of fetch() in the "Side Service" can be found in Fetch API - MDN.

When called by passing in a URL address string, it is a GET request.

const url = 'https://xxx.com/api/xxx'
const { body: { data = {} } = {} } = await fetch(url)

When more request parameters need to be passed, the behavior differs from the standard Fetch API in that all parameters are in a single object and only the url, method, headers, and body attributes are supported.

const { body: { data = {} } = {} } = await fetch({
url: 'https://xxx.com/api/xxx',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Hello Zepp OS'
})
})
tip

For a sample of the Fetch API used in a Mini Program, see FetchAPI.