GeoJSON (POST) Directions error


#1

I am attempting to use the default javascript (or PHP) code example from here - https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/geojson/post - regarding getting a geoJSON route for driving-car, and although using my api key works when I hit the “call action” button and the “API response” on the example page returns both the map route and the GeoJSON code … clicking the download button returns a file that just contains the words [object][object] … and using the exact same code in my own project throws an HTTP 500 error with the following response;

{“error”:{“message”:“Content type ‘text/plain;charset=UTF-8’ not supported”},“info”:{“engine”:{“version”:“5.0.1”,“build_date”:“2019-05-29T14:22:56Z”},“timestamp”:1568626847241}}

just for info the default code used is the following;

let request = new XMLHttpRequest();

request.open('POST', "https://api.openrouteservice.org/v2/directions/driving-car/geojson");

request.setRequestHeader('Accept', 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8');
request.setRequestHeader('Authorization', 'my-api-key-removed');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
  }
};

const body = '{"coordinates":[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]],"instructions":"false"}';

request.send(body);

#2

Can anyone help or tell me if I’m doing something wrong??


#3

Hi @gmspromo - I don’t believe you are doing anything wrong so I will pass this across to the guys that deal with that part of the openrouteservice


#4

I won’t be able to fix this before friday.

Can you try to add ‘text/plain’ to the setRequestHeader(‘Accept’,…
This might resolve it for now.


#5

Nope, sadly this doesn’t seem to fix the issue …


#6

Hey @gmspromo,
If you only want to download the data into a file, here a quick and dirty hack:

  1. open your browser developer tools (f12 or strg + shift + i or from menu)
  2. in the developer tools open the network tab
  3. press the call button to execute your request
  4. look for the POST request to your endpoint and click it
  5. click the response tab and view it as raw (or maybe scroll down, depends on the browser)
  6. paste the content to a new file with the corresponding file ending (.geojson, .json or .gpx)

This will be fixed soon,
best regards,
Amandus


#7

Thanks for pointing out this temporary work around Amandus, it’s much appreciated and helps me out a lot in the short term while I’m testing an app …


#8

Hi @gmspromo,
this issue is resolved
best regards


#9

The issue appears resolved on the website (https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/geojson/post) in so far as you can now download the GeoJSON, but I’m still getting the same API error using the javascript in my original post …


#10

try to set
request.setRequestHeader('Content-Type', 'application/json')
then it should work

cheers


#11

yep, that fixes the API call … thanks for this, you really don’t know just how much this helps me out!