How to Send Data to API in JavaScript Node – Best Practices? or What’s the Right Way to Send Data to

20 Replies, 599 Views

Yo! For how to send data to api in javascript node, `fetch` is solid if you’re on Node 18+ (it’s built-in now!).

```javascript
const response = await fetch('https://api.example.com', {
method: 'POST',
body: JSON.stringify({ data: 'here' }),
headers: { 'Content-Type': 'application/json' }
});

if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const result = await response.json();
console.log(result);
```

Bonus: Check out the MDN docs for `fetch`—they’re gold.

Messages In This Thread



Users browsing this thread: 1 Guest(s)