Issue
Im using flask with Blueprints. I am trying to pass an ID from the url with a post request, but i dont understand the behaviour here:
i am on page localhost/2/updatestrat
This will post to /add_indicator
let indi_data = await postJsonGetData(data, "/add_indicator");
async function postJsonGetData(data, endpoint, method = "POST") {
const options = {
method: method,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
};
let response = await fetch(endpoint, options);
if (!response.ok) {
throw new Error("Request failed");
}
const responseData = await response.json();
return responseData;
}
This will post to /2/load_conditions
const { sell_conds, buy_conds } = await getJson("load_conditions");
async function getJson(endpoint) {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
let response = await fetch(endpoint, options);
if (!response.ok) {
throw new Error("Request failed");
}
const responseData = await response.json();
console.log(responseData, "DDDDDDDD");
return responseData;
}
How come that getJson will include the id of the page in the endpoint and postJsonGetData dont?
I can easily get the data from getJson like this
@bp.route('/<int:id>/load_conditions', methods=['POST'])
def load_conditions(id):
but when i try to do the same with "/add_indicator"
@bp.route('/<int:strategy_id>/add_indicator', methods=('POST',))
@login_required
def add_indicator(strategy_id):
if request.method == 'POST':
print(strategy_id)
i get this error: POST http://127.0.0.1:5000/add_indicator 404 (NOT FOUND)
so clearly it is not posting to the correct endpoint.
Solution
I found the solution. Instead of:
let indi_data = await postJsonGetData(data, "/add_indicator");
i should do:
let indi_data = await postJsonGetData(data, "add_indicator");
Answered By - Soma Juice
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.