-1

This has been marked as a duplicate which is NOT true. This is a mobile app, NOT a web app. There is no originating domain, like the proposed answer has for a web app.

I am trying to use the salesforce api with flutter / dart. I get the following error:

Access to XMLHttpRequest at 'https://instancename.my.salesforce.com/services/data/' from origin 'http://localhost:53765' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

It does not matter if I try to get a session token

void getData() async{
  var headers = {
    'Authorization': 'Bearer access_token',
  };

  var res = await http.get('https://instancename.my.salesforce.com/services/data/v20.0/', headers: headers);
  if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
  print(res.body);
}

or just the version

void getData() async{
  Response response=await get("https://instancename.my.salesforce.com/services/data/");
  print(response.body);
}

everything fails.

user14202986
  • 153
  • 1
  • 1
  • 9

1 Answers1

0

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any other origins (domain, scheme, or port) than its own from which a browser should permit loading of resources. ... For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. you have to set cars policy to your web server not flutter application it happen when you want to connect to you web api on internet if you run your web api on local you'r not get that error

  • There is no browser involved. This is not a web app, this is a mobile app, which functions in a completely different manner. – user14202986 Jun 14 '21 at 10:59