Skip to main content

Sending Data to REST APIs

First, make sure you have the necessary packages installed in your project. This will likely include the react, react-dom, and typescript packages, as well as the axios package for making HTTP requests.

Next, set up your TypeScript and React configuration. In your tsconfig.json file, set the jsx option to react, and in your React component files, include the @ts-ignore directive at the top to disable type checking.

In your component, create a function for sending the request to the API. This function should use the axios library to send a POST or PUT/PATCH request to the desired endpoint.

To send a POST request with a JSON body, you can use the axios.post() function and include the data in the data property of the request configuration object:

async function sendData() {
const response = await axios.post('https://my-api.com/endpoint', {
key1: 'value1',
key2: 'value2',
});
// do something with the response
}

To send a PUT/PATCH request with a JSON body, you can use the axios.put() or axios.patch() function and include the data in the same way:

async function updateData() {
const response = await axios.put('https://my-api.com/endpoint', {
key1: 'new value',
key2: 'new value',
});
// do something with the response
}

If you need to include additional options in the request, such as headers or query parameters, you can include them in the request configuration object as well. For example, to include a Content-Type header in the request, you can do the following:

async function sendData() {
const response = await axios.post('https://my-api.com/endpoint', {
key1: 'value1',
key2: 'value2',
}, {
headers: {
'Content-Type': 'application/json',
},
});
// do something with the response
}