javascriptreact-nativemagentomagento2magento-rest-api

React Native, using Magento 2 REST API.


So I have an e-commerce site set up using Magento 2 on my localhost. I want to request admin token from Magento through REST API in React Native. I tried using the fetch method but I am not able to proceed and I keep getting an error. I tried to request the same thing from a different REST client and its working there. The error is

    Network request failed
onerror
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\whatwg-fetch\fetch.js:441:29
dispatchEvent
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\event-target-shim\lib\event-target.js:172:43
setReadyState
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:567:29
__didCompleteResponse
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:397:25
<unknown>
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:503:16
emit
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:180:12
__callFunction
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:351:47
<unknown>
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:116:26
__guardSafe
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:314:6
callFunctionReturnFlushedQueue
    E:\Javascript\React Native\TestApp02\TestApp03\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:115:17

Here is my code:

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class FetchExample extends React.Component {

  constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
    return fetch('http://localhost/magento/rest/V1/integration/admin/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            "username": "RED_KAY",
            "password": "KGAMER30",
        }),    
    })
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: JSON.stringify(responseJson),
        }, function(){

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }
  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        <Text> {this.state.dataSource} </Text>
      </View>
    );
  }
}

I tried searching for solutions online but wasn't successful. Idk what i am doing wrong. Any help will be greatly appreciated!


Solution

  • It is not clear what is wrong from the information you shared. But you can check my implementation for Magento 2 with React Native https://github.com/troublediehard/magento-react-native/blob/master/src/magento/index.js. It works for me.