1

I'm working on an API written in python using flask but I'm having trouble sending a post request with authorization. I'm using axios to make the requests.

I tried pretty much everything I found on this link [Solve Cross Origin Resource Sharing with Flask]

from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

@app.route("/test", methods=["POST","GET"])
# @cross_origin is commented for the get request
@cross_origin(methods=["POST"], allow_headers=['Content-
def test():
    return jsonify({'success':True})

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
// change post to get
axios.post('http://localhost:5000/test', {
  // withCredentials: true,
  headers: {
    'Access-Control-Allow-Headers': 'Authorization',
    'Authorization': 'token'
  }
})
.then((response) => {
  console.log(response);
})
.catch((error) => {
  console.log(error);
});

The authorization token comes up in the headers when I use get but not post

get request: https://i.stack.imgur.com/SjxdD.png

post request: https://i.stack.imgur.com/eu1OS.png

EDIT (SOLVED?)

not really a solution but I switched to fetch and that solved my issue but I couldn't figure out why it was happening in the first place

pablo
  • 11
  • 2

1 Answers1

0

You just have to change the * with http://localhost:5000/test , you should allow access for a specific domain.

Another way is to initialize flask-cors with default arguments:

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)
Hasni Iheb
  • 282
  • 2
  • 12