To make a HTTP request with a custom method in TypeScript, we can use a library like Axios or the native Fetch API.
Example code to make a PATCH
request using Axios in TypeScript:
index.ts295 chars13 lines
In this example, we import the Axios library and use the patch
method to make a PATCH
request to https://example.com/api/user/1
with the payload of { name: 'John Doe' }
. The response is logged to the console, and errors are handled with a .catch()
block.
Example code to make a PUT
request using Fetch API in TypeScript:
index.ts388 chars20 lines
In this example, we use the native Fetch API to make a PUT
request to https://example.com/api/user/1
with the payload of { name: 'John Doe' }
. The response is parsed as JSON and logged to the console, and errors are handled with a .catch()
block. Note that we need to set the Content-Type
header to application/json
and stringify the data before sending.
gistlibby LogSnag