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

To make an HTTP request with a custom body as HTML in TypeScript, you can use the Fetch API which is a modern replacement for the traditional XMLHttpRequest. The following code demonstrates how to make a HTTP POST request with a custom HTML body using Fetch API in TypeScript:

index.ts
const url = 'https://example.com/api';
const htmlBody = '<html><body>Hello, world!</body></html>';

fetch(url, {
  method: 'POST',
  body: htmlBody,
  headers: {
    'Content-Type': 'text/html'
  }
})
.then(response => {
  // Handle response
})
.catch(error => {
  // Handle error
});
285 chars
17 lines

In the above code, we define the url and htmlBody variables. We then make a fetch() request with the POST method and the htmlBody passed as the body property. We also set the Content-Type header to text/html. Finally, we handle the response or any errors that occur.

Note that the fetch() API returns a Promise so we can use then() and catch() to handle the response or any errors that occur.

gistlibby LogSnag