3

I am trying to put three params in my post request to particular api but i didn't get the response as i expected. API works fine in my Postman but i am not sure about my fetching method in my react native app i am new to this so i don't know how to put headers in my api request i followed some docs but didn't get much please have a look and answer my question.

 constructor (props) {
        super (props)
        this.state = {
            detail: ''
        }
    }

    ComponentDidMount(){
        var data = new FormData();
        data.append('mobile_number','8615351655')
        data.append('mobile_country_code','+21')
        data.append('rec_name','Shantanu Talwaar')
    }
    fetchData = async() => {
        fetch('http://link.com/link/',
        {
            method: 'POST', 
            headers:{
                 //this what's exactly look in my postman
                'Authorization': 'Token 97a74c03004e7d6b0658dfdfde34fd6aa4b14ddb;
            },
            body: this.data
        })
        .then((response) => response.json())
        .then((responseJson) => {
                alert(responseJson.detail)
        }).catch((error) => {
            alert('error')})}

  render() {
    return (
      <View style = {styles.container}>
        <Button onPress = {this.fetchData} title = "fetch"/>
        <Text style={styles.text}>Fetched data displays below</Text>
        <Text style={styles.text}>{this.state.detail}</Text>
      </View>
    )
  }
}

This is the result i am having right now in my alert box: "Authentication credentials were not provided."

2 Answers 2

4

There is a ' missing after your token.

'Authorization': 'Token 97a74c03004e7d6b0658dfdfde34fd6aa4b14ddb;

And as it is a JSON Object you should remove the semi-colon

So, the final code will be

'Authorization': 'Token 97a74c03004e7d6b0658dfdfde34fd6aa4b14ddb'

There is also another problem. The data declaration is not accessible from the fetch function. So you should do something like this.

fetchData = async() => {
    var data = new FormData();
    data.append('mobile_number','8615351655')
    data.append('mobile_country_code','+21')
    data.append('rec_name','Shantanu Talwaar')

    fetch('http://link.com/link/',
    {
        method: 'POST', 
        headers:{
            //this what's exactly look in my postman
            'Authorization': 'Token 97a74c03004e7d6b0658dfdfde34fd6aa4b14ddb'
        },
        body: data
    })
    .then((response) => response.json())
    .then((responseJson) => {
        alert(responseJson.detail)
    }).catch((error) => {
        alert('error')
    })
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have fixed the mistake. The statement inside catch() is getting executed. Any idea what could be wrong ?
I have fixed another problem in your code. About the auth token, you must see the API docs to see how it must be provided. What is the API?
Thanks a lot this worked man! please also upvote me. Thank you
3

i think you can use "x-access-token" as header name for authentication token and place Content-Type too.

fetchData = () => {
        fetch('http://link.com/link/',
        {
            method: 'POST', 
            headers:{
                'Content-Type': "application/json",
                'x-access-token': 'Token 97a74c03004e7d6b0658dfdfde34fd6aa4b14ddb'
            },
            body: this.data
        })
        .then((response) => response.json())
        .then((responseJson) => {
                console.log(responseJson.detail)
        }).catch((error) => {
            alert('error')})
            }

2 Comments

I am sending formData , and receiving JSON as response. Do i need to make any changes to this code ?
now your fetchData method work fine what you need to do next depends upon you application requirement. you can handle response data to show message to user about success/failed to post data.

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.