'Flask POST returning 400 in production
I am building some POST endpoints in my code.
The problem is that they work fine in testing, using pytest with flask test client, but they don't work at all with outside requests originating from cURL or python's requests module.
I have done A LOT of testing and I couldn't figure out why flask is throwing the 400 error. Does anyone have any idea what might be going on?
Endpoint:
@api_blueprint.route("/tibiacoin/confirmation", methods=["POST"])
def tibicoin_withdrawal_confirmation():
content = request.get_json(force=True)
withdrawal_id = content["id"]
withdrawal_status = content["status"]
if withdrawal_status == "OK":
withdrawal_object = TibiaWithdrawal.objects(pk=withdrawal_id)[0]
withdrawal_object.is_processed = True
withdrawal_object.save()
response_dict = {"status": True}
return jsonify(response_dict)
Test pytest:
@pytest.fixture(scope='module')
def create_flask_app():
#drop all records in testDatabase before strting new test module
db = connect(host=os.environ["MONGODB_SETTINGS_TEST"], alias="testConnect")
for collection in db["testDatabase"].list_collection_names():
db["testDatabase"].drop_collection(collection)
db.close()
# Create a test client using the Flask application configured for testing
flask_app = create_app()
return flask_app
@pytest.fixture(scope='function')
def test_client(create_flask_app):
"""
Establish a test client for use within each test module
"""
with create_flask_app.test_client() as testing_client:
with create_flask_app.app_context():
yield testing_client
def test_withdrawal_confirmation(create_flask_app, test_client, data_sample_tibiacoin_api):
payload = {
"id": "",
"status": "OK",
}
response = test_client.post('/api/tibiacoin/confirmation', json=payload, headers=headers)
assert response
assert response.json["status"]
test response:
OK
Actual requests:
import requests
payload = {
"id": "",
"status": "OK",
}
r = requests.post('http://localhost:5010/api/tibiacoin/deposits', json=payload, headers=headers)
print(r.status_code)
response:
400
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
