You can use the fetch
method in JavaScript to make an HTTP request with a custom body as JSON. Here's an example:
index.tsx315 chars13 lines
In this example, we are making a POST
request to the URL https://example.com/api/endpoint
with a JSON object as the request body. The fetch
method accepts two parameters: the URL and an object containing request options. In the request options object, we set the method
to POST
, the Content-Type
header to application/json
, and the request body to the JSON string representation of our data using the JSON.stringify()
method.
After making the request, we parse the response by calling the json()
method on the response object. Finally, we log the response data to the console or catch any errors that occurred during the request.
gistlibby LogSnag