make a http request with a custom body as text in typescript

To make an HTTP request with a custom body as text in TypeScript, you can use the Axios library. Here's an example:

index.ts
import axios from 'axios';

const requestBody = 'This is the custom body';
const requestConfig = {
  headers: { 'Content-Type': 'text/plain' }
};

axios.post('https://example.com/api/endpoint', requestBody, requestConfig)
  .then(response => console.log(response.data))
  .catch(error => console.error(error));
311 chars
11 lines

In this example, we're making a POST request to https://example.com/api/endpoint with a custom body of This is the custom body. We're also specifying a request configuration object that sets the Content-Type header to text/plain.

If the request is successful, the response data will be logged to the console. If there's an error, the error will be logged to the console.

Note that you'll need to have Axios installed in your project (npm install axios or yarn add axios).

gistlibby LogSnag