skip to Main Content

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:JavascriptReact NativeTestApp02TestApp03node_moduleswhatwg-fetchfetch.js:441:29
dispatchEvent
    E:JavascriptReact NativeTestApp02TestApp03node_modulesevent-target-shimlibevent-target.js:172:43
setReadyState
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:567:29
__didCompleteResponse
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:397:25
<unknown>
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:503:16
emit
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesvendoremitterEventEmitter.js:180:12
__callFunction
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:351:47
<unknown>
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:116:26
__guardSafe
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:314:6
callFunctionReturnFlushedQueue
    E:JavascriptReact NativeTestApp02TestApp03node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.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!

2

Answers


  1. Can you try with this url

    http://localhost/magento/index.php/rest/V1/integration/admin/token
    

    Source: http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search