I'm struggling to understand why this request is giving me a CORS error. I believe I have both the server and client setup to consume the endpoint properly. I can make the same request with postman and it works, so I think there is something I'm doing wrong with JS. The server is using Fastapi, a python framework. The client is Vue but the request uses Axios (which is Javascript). Any insight would be appreciated.
Server Settings
app = FastAPI(title="OptionStrats",
description="For fun fin-trial",
version="0.0.1",)
origins = ["*", "http://localhost:8080/"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Client Request
axios({
method: "post",
url: "http://optionstrats.com/backtest",
headers: {
common: {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
"Access-Control-Allow-Origin": "*"
},
data: request_dict,
crossDomain: true
})
.then(response => {
console.log("backtest response", response);
})
.catch(error => {
console.log(error);
});
Logs
Starting Request
Object { url: "http://optionstrats.com/backtest", method: "post", data: {…}, headers: {…}, transformRequest: (1) […], transformResponse: (1) […], timeout: 0, adapter: xhrAdapter()
, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
Backtest.vue:464
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://optionstrats.com/backtest. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Error: "Network Error"
createError createError.js:16
handleError xhr.js:81
I'm explicitly including Access-Control-Allow-Origin in the axios request headers but it seems to not be understood. What am I doing wrong? Thanks.