4

I am trying to call and API with react app(TSX) using Axios(this the first time I am using Axios) every time I run the app the method changes to 'OPTIONS' and the request becomes invalid. Help will be appreciated. Sharing my code sorry I am hiding the Auth Token for security reasons.

Code

import React, { useState, useEffect } from 'react';
import axios from 'axios';

interface Brands {
  BrandId: number;
  Name: string;
}
const AUTH_TOKEN = Something hiden for security;


var baseUrl = axios.defaults.baseURL = 'https://fppdirectapi-prod.fuelpricesqld.com.au/Subscriber/GetCountryBrands?countryId=21';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.get['Content-Type'] = 'application/json';
axios.defaults.method = 'get';

const FetchFuelType = () => {
  const [brands, setPosts] = useState<Brands[]>([]);

  useEffect(() => {
    axios.get(baseUrl)

      .then(res => {
        console.log(res)
        setPosts(res.data)
      })
      .catch(err => {
        console.log(err)
      })
  }, [])
  return (
    <div>
      <ul>
        {brands.map(Brand => (<li key={Brand.BrandId}>{Brand.Name}</li>))}
      </ul>
    </div>
  );
};

export default FetchFuelType;

Attached image of response enter image description here

Krishneil
  • 1,432
  • 1
  • 18
  • 26

3 Answers3

12

OPTIONS request is part of the so-called preflight request which is needed to figure out the CORS headers to know what needs/is allowed to be sent to the server with the actual GET request. Thats why you normally see two requests in your network tab (depending on your setting)

In your example it seems you have not configured anything CORS related on your server (thus the 405) or specifically have forbidden anything other than GET/POST requests. Or potentially the site has forbidden others to access its data

japrescott
  • 4,736
  • 3
  • 25
  • 37
3

The OPTIONS request is an inherent request generated by browser. Browser uses the OPTIONS call to find out what methods are allowed by the server.

The API server meant for supporting requests from browser must allow OPTIONS in addition to the actual method (GET / POST / etc).

If the server does not support OPTIONS, then it may not support browser.

A server that does not support OPTIONS can only support non-browser clients (Examples: REST client in Java / nodejs)

How to solve the problem?

The problem of '405 - OPTIONS method not allowed' can be solved in one of these 2 ways:

  1. Update the server to support OPTIONS method (Recommended for server that is supposed to support browsers)

  2. Develop an 'Intermediary REST client' which will request data from the server, on behalf of the browser

Browser <--> REST client (supports OPTIONS, POST) <--> Actual web service (does not support OPTIONS)

Gopinath
  • 4,066
  • 1
  • 14
  • 16
2

Usually, options request is sent before get automatically, to get some preliminary data before firing get call. Check this https://github.com/axios/axios/issues/475.

Praveenkumar
  • 2,056
  • 1
  • 9
  • 18