Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cors Preflight #30

Closed
minute-med opened this issue May 26, 2023 · 1 comment · Fixed by #33
Closed

Cors Preflight #30

minute-med opened this issue May 26, 2023 · 1 comment · Fixed by #33

Comments

@minute-med
Copy link

minute-med commented May 26, 2023

i tried using this package in my vue 3 app

const v = new Valhalla();

v.directions(
    [
        [47.380742, 8.512516], // pass as [lat, lon]
        [47.359467, 8.557835],
    ],
    "auto"
).then((d) => {
    console.log(d)
    // do stuff with the directions response
    d.directions.forEach((direction) => {
        console.log(direction.feature) // get the route as GeoJSON feature
        console.log(direction.feature.properties.distance) // get the route distance
        console.log(direction.feature.properties.duration) // get the route duration
    })
})

the method directions launch an http request which trigger CORS preflight because the request headers aren't considered "simple" (see mdn doc and Valhalla server accept exclusively GET and POST requests so if the browser trigger an OPTION (because your request isnt "simple") it will fail

the following code work

delete axios.defaults.headers.common;
axios.defaults.headers.common = {'Accept': '*/*'};

const t = encodeURI('{"locations":[{"lon":104.9224426,"lat":10.6314754},{"lon":104.132637,"lat":10.6314754}],"costing":"auto","narrative":false}');
axios.get(`http://localhost:8002/route?json=${t}`, {
    headers: {'Accept': '*/*'},
}).then((res) => { console.log(res) })`

or POST

const a = axios.create()
delete a.defaults.headers.post;
delete a.defaults.headers.common;

a.post(`http://localhost:8002/route`, {
    "locations":[
        {"lon":104.9224426,"lat":10.6314754},{"lon":104.132637,"lat":10.6314754}
    ],
    "costing":"auto",
    "narrative":false
},
{
    headers: {
        'Content-Type': 'text/plain'
    },
}).then((res) => { console.log(res) })

I've tried with both the public endpoint and the self hosted version.

@chrstnbwnkl
Copy link
Member

I feel like this is the same as #25 . Probably solved by simply removing the User-Agent header, do you want to draft a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants