Here's an example of making an HTTP request with a custom header using the fetch
API in JavaScript:
index.tsx209 chars9 lines
In this example, we are making a GET
request to https://example.com/api/data
with a custom header named Custom-Header
and a value of custom-value
. Once the response is received, we are parsing its JSON representation and logging it to the browser console. If there's an error fetching the data, we log the error to the console as well.
You can also use the XMLHttpRequest
(XHR) object to make HTTP requests with custom headers. Here's how:
index.tsx363 chars13 lines
In this example, we are creating a new XHR object, opening a GET
request to https://example.com/api/data
, setting a custom header using the setRequestHeader
method, defining a callback function to handle the response, sending the request using the send
method, and parsing the JSON representation of the response data. If the request fails, we log an error message to the console.
gistlibby LogSnag