ORS directions with avoid polygons parameter

Can i call the API like: https://api.openrouteservice.org/v2/directions/driving-car?api_key=your-api-key&start=x,y&end=x,y, and declare the avoid polygons function at that API call? If yes, how can i implement it?

Thanks in advance

Hey,

no, the GET request does not support anything but the means of transport, start and end point.
To use the avoid_polygons-parameter, you’ll have to use a POST request.

Have a look at the api documentation to see how this will work.

Best regards

Are you aware of how i could implement that? I tried it like so:

Future getData() async {
    var url3 = Uri.parse('https://api.openrouteservice.org/v2/directions/driving-car/');
    var response = await http.post(url3, body: {'coordinates': '[[startLng,startLat],[endLng,endLat]]'});
     
    if(response.statusCode == 200){
      String data = response.body;
      print(response.statusCode);
      return jsonDecode(data);
    }else{
      print(response.statusCode);
    }
  }

but i get an error, XMLHttpRequest error. What could be the issue? I am implementing this in Dart,Flutter.

You have to pass your API key in the Authorization Header.

Also the content of the Error would be helpful.

Best regards

This is my updated code:

 Future getData() async {
    var url3 = Uri.parse('https://api.openrouteservice.org/v2/directions/driving-car/geojson');
    Map<String,String> header = {"Authorization":apiKey};
    var response = await http.post(url3,headers: header,body: jsonEncode({'coordinates': [[startLng,startLat],[endLng,endLat]]}));
     
 if (response.statusCode == 200){
      String data = response.body;
      print(response.statusCode);
      return jsonDecode(data);
    }else{
      print(response.statusCode);
    }

The error is:

[]
500
NoSuchMethodError: '[]'
Dynamic call of null.
Receiver: null
Arguments: ["features"]

Any help is appreciated, thank you

probably an issue with the jsonEncode function that treats the square brackets as a method.
Or a problem with the jsonDecode as the argument is “features” wich is returned in a valid ORS response.
I don’t get why it prints 500 then though.

Best use breakpoints instead of print debugging, so you can inspekt the exact object of data and treat it accordingly. Also you then realize exactly where something goes wrong.

Hello again,

I managed to make the POST http request work. Now my issue is that i cannot command the request to include options in order to avoid polygons. For ex:

http.Response response = await http.post((Uri.parse('https://api.openrouteservice.org/v2/directions/driving-car/geojson')),
     headers: <String, String>{
      'authorization':apiKey,'Content-Type':'application/json'
    },
    body: jsonEncode(<String, List>{
      "coordinates": [[startLng,startLat],[endLng,endLat]],"options"
    }),
   );

but i receive an error every time i try to include the parameter options. How should i correct this? I tried the way the API recommends