To make HTTP requests to the JSONPlaceholder API in TypeScript, follow these steps:
Install the axios
library by running npm install axios
or yarn add axios
.
Import axios
into your TypeScript file:
index.ts27 chars2 lines
Define the type of your data. Assuming you're making a GET request to the /todos
endpoint, you can define the type like this:
index.ts88 chars7 lines
Make the request using the axios.get()
function:
index.ts157 chars6 lines
In this example, we're making a GET request to the /todos
endpoint and expecting an array of Todo
objects as a response. We then log the todos
array to the console.
You can also make other types of requests (POST, PUT, DELETE) using the relevant axios
functions (axios.post()
, axios.put()
, axios.delete()
).
gistlibby LogSnag