1

I am trying to call the web API for products in Magento from a React Native app. After other stack exchange question & answers plus tutorials I am still receiving a 401 response. I know the call works because I can make it through postman.

Update: So I did not solve it, however I have found that if I use axios the request will work. This seems to be an issue with fetch.

.htaccess mod_headers.c settings

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
Header set Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS"

app/etc/env.php

'x-frame-options' => 'CROSS-ORIGIN'

API Call from React Native

fetch(
      'http://localhost:8888/magento/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][value]=product name',
      {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Authentication: 'Bearer fwynACCESS_TOKENbal9tfr'
        }
      }
    )
      .then((res) => {
        if (res.status !== 200 && res.status !== 204)
          reject({ message: 'There was an error with the products service' })
        resolve(res.json())
      })
      .catch((err) => reject(err))

This also includes a web integration named Customers with all the API options set as accessible.

NicholasByDesign
  • 781
  • 1
  • 11
  • 33

0 Answers0