md5hash
md5hash

Reputation: 63

How to send HTTP headers in ReactJS using fetch()

const URL = "http://url.com";
fetch(URL).then(res => res.json()).then(json => {
    this.setState({ someData: json });
});

How to send HTTP request with HTTP headers?

Upvotes: 5

Views: 24164

Answers (3)

Y2H
Y2H

Reputation: 2537

Inside the fetch() method you should do something like this

fetch(url, {
    ...    
    headers: {
       'user-agent': 'Mozilla/4.0 MDN Example',
       'content-type': 'application/json'
    }

For more details, look at the Mozilla Developers documentation.

Upvotes: 3

Colin Ricardo
Colin Ricardo

Reputation: 17249

You can just pass them into fetch():

const API = 'foo';

fetch(API, { headers: {
  'user-agent': 'Mozilla/4.0 MDN Example',
  'content-type': 'application/json'
}}).then()

You can read more on that here.

Upvotes: 4

Karthik S
Karthik S

Reputation: 313

Try this

fetch('your_url', { 
   method: 'get', 
   headers: new Headers({
     // Your header content
   })
 });

Upvotes: 4

Related Questions