Hello all, I am new to web development and I am currently facing some issues regarding access to the content of my confluence page using REST API calls in Flutter. Whenever I try to make the call, I get back the error ‘XMLHTTPRequesterror’ and when I inspect my console it tells me that there is a CORS rule not permitting the access. So I wish to know more and how I can proceed. Below I provide my code which works in Postman
and the error message.class ContainerTitleApi extends StatefulWidget {
@override
_ContainerTitleApiState createState() => _ContainerTitleApiState();
}
class _ContainerTitleApiState extends State<ContainerTitleApi> {
@override
Widget build(BuildContext context) {
return Center(
child: FutureBuilder<Post>(
future: fetchPost(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text("${snapshot.data.title}");
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
);
}
}
class Post {
final String title;
Post({this.title,});
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
title: json['title'],
);
}
}
Future<Post> fetchPost() async {
var headers = {
'Authorization': 'Basic *********'
};
var request = http.Request('GET', Uri.parse('http://proceedit.atlassian.net/wiki/rest/api/content/2413035961/child/page?expand=body.view.value'));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.stream.toString()));
}
else {
throw Exception(response.reasonPhrase);
}