I am trying to make a post request to an API in flutter and it works perfectly when I use cURL on the command line but the request in flutter doesn't work. Tried a ton of different request formats but nothing works.
Here is the working cURL request: (the API correctly returns a string with details about the YouTube video)
curl -m 70 -X POST <API_URL_HERE> \
-H "Content-Type:application/json" \
-d '{"youtube_link":"https://www.youtube.com/watch?v=gbUfbN7vT20"}'
Here is the not working Flutter request:
String link = "https://www.youtube.com/watch?v=gbUfbN7vT20";
var url = Uri.parse("<API_URL_HERE>");
var response = await http.post(url,
body: jsonEncode({"youtube_link": "$link"}),
headers: {"Content-Type": "application/json"}
);
Error: XMLHttpRequest error. I checked on the server that is receiving the request. With the good request it returns a status code of 200 and on the Flutter request it returns a status code of 400 (but no errors are occurring in the actual code, it seems to be just a problem with the request).
Update: Ok, it seems it was a CORS issue. However I haven't managed to fix it on the API side either by following these instructions https://cloud.google.com/functions/docs/writing/http#handling_cors_requests
although that is probably a separate question.