# node fetch

***

### 1. Sending a GET request

```javascript
fetch('https://example.com/api/v1/users')
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 2. Sending a POST request

```javascript
fetch('https://example.com/api/v1/users', {
  method: 'POST',
  body: JSON.stringify({
    name: 'John Doe'
  })
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error submitting form');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 3. Sending a request with headers

```javascript
fetch('https://example.com/api/v1/users', {
  headers: {
    'Content-Type': 'application/json'
  }
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 4. Sending a request with credentials

```javascript
fetch('https://example.com/api/v1/users', {
  credentials: 'include'
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 5. Sending a request with a timeout

```javascript
fetch('https://example.com/api/v1/users', {
  timeout: 5000
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 6. Sending a request with a redirect

```javascript
fetch('https://example.com/api/v1/users', {
  redirect: 'follow'
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 7. Sending a request with a body

```javascript
fetch('https://example.com/api/v1/users', {
  body: JSON.stringify({
    name: 'John Doe'
  })
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 8. Sending a request with a stream body

```javascript
fetch('https://example.com/api/v1/users', {
  body: fs.createReadStream('file.txt')
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 9. Sending a request with a form body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new FormData()
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 10. Sending a request with a URLSearchParams body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new URLSearchParams({
    name: 'John Doe'
  })
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 11. Sending a request with a Blob body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new Blob(['Hello world!'], {
    type: 'text/plain'
  })
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 12. Sending a request with an ArrayBuffer body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new ArrayBuffer(16)
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 13. Sending a request with a File body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new File(['Hello world!'], 'file.txt')
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }
  })
  .then(data => {
    // Data received successfully
  })
  .catch(error => {
    // An error occurred
  });
```

### 14. Sending a request with a ReadableStream body

```javascript
fetch('https://example.com/api/v1/users', {
  body: new ReadableStream()
})
  .then(response => {
    if (response.ok) {
      // Request successful
      return response.json();
    } else {
      // Request failed
      throw new Error('Error fetching API data');
    }

```
