0
fetch("http://localhost/user/shop", {
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'text/plain'
  }
})
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  });

Hello, everyone. I'm getting a 403 forbidden request error from calling an API from a local backend server. The API call works in Postman flawlessly but for some reason, when I use a fetch/axios GET request in React Native to call the API, it does not work and keeps giving me that 403 error. Does anybody have a solution for this? All I'm doing is just a simple GET request that shouldn't require any authorization details or anything. Below are screenshots of the error and the Postman request:

enter image description here

enter image description here

enter image description here

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Sure. Please see my above edited post with screenshots. :) – Stanley Yeung Dec 09 '21 at 02:41
  • 1
    Where is the react-native app running? If it's on a device that is not the same host as the backend server, you will need to use the network address of the server, not `localhost` – Phil Dec 09 '21 at 02:49
  • FYI, GET requests do not require a content-type header since they have no request body. You also seem to be double JSON encoding the `data` in the response which you probably don't want to do – Phil Dec 09 '21 at 02:51
  • The react-native app is running on http://localhost:19006 through Expo. My Java Spring backend is running on http://localhost:80 – Stanley Yeung Dec 09 '21 at 02:53
  • 1
    @StanleyYeung but what device is running the react-native app? Is it on your phone or are you opening the web version in your PC browser? – Phil Dec 09 '21 at 02:58
  • Okay. I just added a screenshots of my React Native app from Visual Studio Code and headers from Postman. :) – Stanley Yeung Dec 09 '21 at 02:59
  • @Phil It's running on my laptop. So yes, it's web version. – Stanley Yeung Dec 09 '21 at 03:01
  • Your Postman request appears to include a session cookie. Are you sure you're not using any authentication? – Phil Dec 09 '21 at 03:20
  • Thanks, Phil! I called the API using Expo app on my phone and it worked! :) (I entered the IP instead of localhost) – Stanley Yeung Dec 09 '21 at 03:23

0 Answers0