make a http request with a custom body in typescript

You can use the axios library to make HTTP requests with a custom body in TypeScript. Here's an example:

First, install the axios library:

npm install axios
18 chars
2 lines

Then, import axios and use it to make a request with a custom body:

index.ts
import axios from 'axios';

const url = 'https://example.com/api/items';

const body = {
  foo: 'bar',
  baz: 42,
};

axios.post(url, body)
  .then(response => {
    // Handle success
    console.log(response.data);
  })
  .catch(error => {
    // Handle error
    console.error(error);
  });
293 chars
19 lines

In this example, axios.post is used to send a POST request to https://example.com/api/items with a custom body consisting of an object with the keys foo and baz. The server's response data is logged to the console on success, and any errors are logged to the console on failure.

gistlibby LogSnag