Ask Your Question
3

What is the proper way of assigning Headers to a fetch request using Javascript?

asked 2021-08-05 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-03-08 20:00:00 +0000

nofretete gravatar image

Headers can be assigned to a fetch request using the Headers constructor and the fetch API:

const myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');

fetch('https://example.com', {
  method: 'POST',
  headers: myHeaders,
  body: JSON.stringify({ key: 'value' })
})
.then(response => {
  // handle response
})
.catch(error => {
  // handle error
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-05 11:00:00 +0000

Seen: 7 times

Last updated: Mar 08 '23