Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions snippets/javascript/api-utility/delete-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Delete Data
description: Delete data trough api
author: capu25
tags: api,delete
---

```js
const deleteData = async (id) => {
const endpoint = `https://api.example.com/users/${id}`;
try {
const response = await axios.delete(endpoint);
console.log(`Deleted user ${id}:`, response.data);
return response.data;
} catch (error) {
console.error(`Error deleting user ${id}:`, error);
throw error;
}
};


// Usage:
const userId = "123";
deleteData(userId); //be sure to have AXIOS imported

```
24 changes: 24 additions & 0 deletions snippets/javascript/api-utility/get-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Get Data
description: Fetch data from an API endopint
author: capu25
tags: api,fetch,get
---

```js
const getData = async (endpoint) => {
try {
const response = await axios.get(endpoint);
console.log("Fetched Data:", response.data);
return response.data;
} catch (error) {
console.error("Error occurred: ", error);
throw error;
}
};

// Usage:
const endpoint = "https://api.example.com/data";
getData(endpoint); //be sure to have AXIOS imported

```
25 changes: 25 additions & 0 deletions snippets/javascript/api-utility/post-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Post Data
description: Post data trough api
author: capu25
tags: api,post
---

```js
const postData = async (endpoint, data) => {
try {
const posteData = await axios.post(endpoint, data);
console.log("Posted Data:", posteData.data);
return posteData.data;
} catch (error) {
console.error("Error occurred: ", error);
throw error;
}
};

// Usage:
const endpoint = "https://api.example.com/data";
const data = { key: "value" };
postData(endpoint, data); //be sure to have AXIOS imported

```