0

i want to call the basic authentication rest api in the react native.how to pass basic authentication headers.getting error - unhandled promise rejection - cant find base64 variable

  getDataFromAPI=async()=>
{

  var headers = new Headers();
  headers.append("Authorization", "Basic " + base64.encode("admin:admin@123"));
  const endpoint='url'
  const res=await fetch (endpoint,{headers:headers})
  const data=await res.json()
  this.setState({items:data})
}
  _renderItem=({item,index})=>{
    let{cardText,card,cardImage}= styles
return  (
  <TouchableOpacity style={card}>
          <Text style={cardText}>{item.id} Reactor Status:  {item.ph}</Text>
  </TouchableOpacity>
)
}
krishna mohan
  • 111
  • 3
  • 10

2 Answers2

0

installed npm i -S base-64 and imported import {decode as atob, encode as btoa} from 'base-64'

and i got the response from the basic authentication rest api in react native code

getDataFromAPI=async()=>
{

  var headers = new Headers();
  headers.append("Authorization", "Basic " + btoa("admin:admin@123"));
  const endpoint='url'
  const res=await fetch (endpoint,{headers:headers})
  const data=await res.json()
  this.setState({items:data})
}
krishna mohan
  • 111
  • 3
  • 10
0

Firstly, it depends on what authentication method you are using at server side but as of above code you can try below

    fetch("https://www.majhgaon.com/app/supplier/add_report", {
            method: "POST",
            headers: {
              'Accept': 'application/json',
              "key-name": "kay-value",
            },
          body: JSON.stringify({
                  ....
               }) 
          })
Mayuresh Patil
  • 2,072
  • 1
  • 21
  • 44