5
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?

Y2H
  • 2,419
  • 1
  • 19
  • 37
md5hash
  • 63
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [Fetch GET Request with custom headers ReactJS](https://stackoverflow.com/questions/42862202/fetch-get-request-with-custom-headers-reactjs) – SomeGuyOnAComputer Apr 29 '18 at 19:00

3 Answers3

4

Try this

fetch('your_url', { 
   method: 'get', 
   headers: new Headers({
     // Your header content
   })
 });
Karthik S
  • 313
  • 2
  • 7
4

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.

Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80
3

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.

Y2H
  • 2,419
  • 1
  • 19
  • 37