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?

1

3 Answers 3

4

Try this

fetch('your_url', { 
   method: 'get', 
   headers: new Headers({
     // Your header content
   })
 });
Sign up to request clarification or add additional context in comments.

3 Comments

Why use new Headers here? Why don't you just use headers: { ... }?
Check github.github.io/fetch/#Headers, The option can take either object or Headers. Maybe fetch api internally converts object to Headers. Thus i have passed direct Headers to fetch option.
@konekoya Array or Headers object both are acceptable
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.