I have a react-native application, when i run locally using expo it is able to call the apis which i developed using flask and deployed on vercel. But when i build the apk of the application to run on my device it can still call the GET urls but not POST urls. I am passing all the parameters, body as required by POST method. Here is the example:
@app.route('/test_api', methods=['GET', 'POST'])
def test_api():
print("# ---- test api ---- #")
if request.method == 'GET':
testData = db.child('test').get()
to_send_list = []
if testData:
to_send_list.append(testData.val())
return jsonify({'data': to_send_list, 'msg':'Got the data' ,'status_code': 200}), 200
else:
return jsonify({'data': [], 'msg':'No data','status_code': 400}), 400
elif request.method == 'POST':
data = request.get_json()
to_send_list = [data]
testData = db.child('test').push(data)
if testData:
return jsonify({'key': testData['name'], 'msg': 'test data pushed', 'status_code': 200}), 200
else:
return jsonify({'key': '', 'msg': 'test data cannot be pushed', 'status_code': 417}), 417
Now when i call this api using this function:
const submitSession = async () => {
setIsRunning(false);
await AsyncStorage.removeItem("sessionStartTime");
console.clear()
const currentDate = new Date();
console.log("Current Date: ", currentDate)
// const formattedDate = currentDate.toISOString().split("T")[0];
const formattedDate = currentDate.toLocaleDateString("en-CA")
const today_new = format(new Date(), "MMM dd, yyyy");
const sessionData = {
date: formattedDate,
day: id,
name: subHead,
exercise: workout
? workout.reduce((acc, item) => {
acc[item.exercise] = checkedItems[item.id] ? "Done" : "Not Done";
return acc;
}, {})
: {},
totalTimeTaken: `${Math.floor(timer / 60)} min ${timer % 60} sec`,
sessionTime: isAM ? "Morning" : "Evening",
};
console.log("Session Data: ")
console.log(sessionData)
const resp = await fetch(uploadSessionUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sessionData),
});
if (resp.ok){
const result = await resp.json();
setResponseModal({
visible: true,
message: "Data successfully added!",
statusCode: "200"
})
}
else {
setResponseModal({
visible: true,
message: "Data push Failed!",
statusCode: "404"
})
}
};
I tried most of the methods i can. Like remakeing the page again, trying a test api and all but the issue still persits in POST API calls only. Any help would be appreciated.