0

I have an API which needs headers to allow access.

It requires 3 types of header, and without them, in a browser you see

Code: 4101
Message: Header X-Candy-Platform is required

But when you have the headers you get a json. Im trying to get the Json in react Native using this

getPostsFromApiAsync(number) {
    return fetch('http://THEAPI?api_key=APIKEY', {method: 'GET', headers: {
                'x-candy-platform': 'desktop',
                'x-candy-audience': 'domestic',
                accept: 'application/json'
            }})
        .then((response) => response.json())
        .then((responseJson) => {
            var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2})
            this.setState({peopleDataSource: ds.cloneWithRows(responseJson)});
        })
        .catch((error) => {
            console.error(error);
        });
}

However this gets me Network request failed.

If I have a file inside 'fetch' which is local, it works fine.

The Three headers required are

x-candy-platform - desktop
x-candy-audience - domestic
Accept - application/json

Any ideas? Thanks

7

1 Answer 1

0

Usually you need a Content-Type header also. Try this and see if it works:

getPostsFromApiAsync(number) {
    return fetch('http://THEAPI?api_key=APIKEY', {method: 'GET',
      headers: {
                'Content-Type'    : 'application/json',
                'Accept'          : 'application/json',
                'x-candy-platform': 'desktop',
                'x-candy-audience': 'domestic'
            }})
        .then((response) => response.json())
        .then((responseJson) => {
            var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2})
            this.setState({peopleDataSource: ds.cloneWithRows(responseJson)});
        })
        .catch((error) => {
            console.error(error);
        });
}
Sign up to request clarification or add additional context in comments.

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.