Sami

In Wix Velo, you can make an HTTP request using the fetch function. Here is an example of how to make a GET request to an API endpoint:

import {fetch} from 'wix-fetch';

async function getData() {
    const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
    const json = await response.json();
    console.log(json);
}

In this example, the fetch the function is used to make a GET request to the specified API endpoint. The response from the API is then converted to JSON format and logged to the console. You can also use fetch to make POST, PUT, and DELETE requests by passing in a second argument with the request options, for example:

const options = { method: 'POST', body: JSON.stringify({ name: 'John' }) };
const response = await fetch('https://jsonplaceholder.typicode.com/posts', options);
Note that the fetch function returns a promise, so you need to use await to handle the response as seen above.
Also, you may need to handle error that may occur during the fetch request.

try {
    const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
    const json = await response.json();
    console.log(json);
} catch (error) {
    console.log(error);
}

Leave a Reply

Your email address will not be published. Required fields are marked *