Reputation: 63
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
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
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
Reputation: 313
Try this
fetch('your_url', {
method: 'get',
headers: new Headers({
// Your header content
})
});
Upvotes: 4