React Native Rest Get Request with Credentials Example

Import required libraries


import axios from 'axios';
import {Buffer} from '@craftzdog/react-native-buffer';

Build your axios header


    const username = 'username';
    const password = 'password';
    
    // Encode username and password in base64
    const encoded = Buffer.from(username + ':' + password).toString('base64');
    console.log(encoded);
    let axiosConfig = {

        headers: {
          'Accept': 'application/json',
          'Authorization': 'Basic ' + encoded
    },
      };

Fetch data


      const fetchData = async () => {
        try {
          const response = await axios.get(API_URL,axiosConfig);
          
          console.log(response.data);
    
        } catch (error) {
          console.error('Error fetching data: ', error);
        }
      };

Trigger it in your hook


    useEffect(() => {
        fetchData();
      }, []);

https://axios-http.com/docs/intro

https://www.npmjs.com/package/@craftzdog/react-native-buffer