Is API back-end ok?

Hello all!

I am new to open route service (and maps stuff in general) and I am trying to make a query to grab the distances matrix.

I am using axios and the request data looks like this:

    Starting Request {
      url: 'https://api.openrouteservice.org/v2/matrix/foot-walking?api_key=MYKEY',
      method: 'post',
      data: '{"metrics":["distance"],"units":"m","locations":[[9.70093,48.477473],[9.207916,49.153868],[37.573242,55.801281],[115.663757,38.106467]]}',
      headers: {
        common: { Accept: 'application/json, text/plain, */*' },
        delete: {},
        get: {},
        head: {},
        post: { 'Content-Type': 'application/json;charset=UTF-8' },
        put: { 'Content-Type': 'application/x-www-form-urlencoded' },
        patch: { 'Content-Type': 'application/x-www-form-urlencoded' },
        'Content-Type': 'application/json;charset=UTF-8',
        'access-control-allow-origin': '*'
      },
      transformRequest: [ [Function: transformRequest] ],
      transformResponse: [ [Function: transformResponse] ],
      timeout: 0,
      adapter: [Function: httpAdapter],
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      validateStatus: [Function: validateStatus]
    }

However, this does not return at all anything, not even an error. If I do the request with POSTMAN it works ok. Therefore, it seems there is something axios adds into the header that upsets the back-end API. When I have seen this are in scenearios where you have an async function and you do not “await” but I use await for request, so no clue what is going on.

Does anyone see what could that be?

Thank you in advance and kind regards.

Hi @javierguzman,

I don’t have experience with the axios stuff - maybe someone like @amandus or @amoncaldas would have a bit more knowledge of that. I believe we do have other users who use Axios and openrouteservice together…

1 Like

Hey @javierguzman,
First of all, we strongly recommend you to use our ors-js library to easy the process of interacting with the API using javascript.

Nevertheless I’ve written a code example and it worked out for me. Please check if it works for you.

import axios from 'axios'

const httpOrsApi = axios.create({
  headers: {
    common: { Accept: 'application/json, text/plain' }
  }
})
let url = 'https://api.openrouteservice.org/v2/matrix/foot-walking?api_key=mykey'
let body = {'metrics': ['distance'], 'units': 'm', 'locations': [[9.70093, 48.477473], [9.207916, 49.153868], [37.573242, 55.801281], [115.663757, 38.106467]]}

httpOrsApi.post(url, body).then(response => {
  console.log(response.data) // at this point the API response brings the expected data
}).catch(err => {
  console.log(err)
})

I hope this help you.

Cheers

1 Like

Hello @amoncaldas!

Thanks for your reply. I have seen that library but right now I only need the matrix feature that is why I am not using the whole package. If in future I need more features, which is quite possible, I will use it! :slightly_smiling_face:

I have tried to run your code but it does not really work. The only variation I have done is the body variable, I do not really know how you manage to run that on Javascript as for me it complains about syntax… So what I do is to create an object and then use stringify function.

I have created the following sandbox (openroute.test.ts file):

It is interesting because in the sandbox I do a console.log(err.response) and it seems I am getting a server internal error. However, when I run locally rather than from codesandbox I do not even get the error.

Regards

Dear @javierguzman.

I tested the code that I pasted here locally before sending to you. It worked.

Now I tested the code you created on sandbox, based on the sample I provided, and it worked after I removed the JSON.stringify body wrapping. An object must be submitted as body, not a string. Right as in the example I wrote to you before.

The line stayed like this:
.post(url, body) and then it worked.

Kind regards,

1 Like

Thanks Amoncaldas!

Indeed it works now in codesandbox :slight_smile: However I have copied and pasted the same code and ran it locally and it did not work. It is the first time this happens to me…anyway, I will continue looking what is wrong and I will post here when I find what is the problem with my local setup, just in case it helps someone in the future.

Again thank you for your help.

Regards

edit: it works now fully! I was worried at the beginning about this Axios not returning response for POST on 500 error · Issue #1143 · axios/axios · GitHub but at the end it turned out to be a stupid error I missed, which is forgetting to place some async/await in some other function who was calling my code, all clear now!

Hello again @amoncaldas,

I was too quick saying it works all. Unfortunately I have realized that distances field is an array full of arrays with 0. When I try with codesandbox is the same. The screenshot you sent does not show distances field unwrapped. Please, could you check it and see if you get all the zeroes as well?

Hey @javierguzman,

The code sample I presented was to show that there is no known issue using the axios client with the ORS api. The replies were based on the point: “this does not return at all anything, not even an error”. So I think we manage to demonstrate that it is returning data when a valid request is made.

Following this, I would then ask you to check our API Playground app. https://openrouteservice.org/dev/#/api-docs/v2/matrix/{profile}/post. There you can try all the possible parameters and check the results instantly.

I think/hope that by exploring the Playground you can clarify your questions about parameters and responses.

Cheers,

@amoncaldas I already played with the API Playground but I still do not understand the rationale why the return values are 0 when I select walking. Is it because is not possible to walk over those locations(no path)? Is it because is too much for walking?

I just tried to run a request in the API Playground using the default request parameters and only changing the profile to foot-walking https://openrouteservice.org/dev/#/api-docs/v2/matrix/{profile}/post and I did get a matrix of zeros. I don’t know why this is happening. Can you help us @adam? Do you know why this is happening?

@javierguzman @amoncaldas,
I think this is due to overstepping distance limits of matrix for that profile.

For foot- profiles try points that are closer:

{"locations":[[9.70093,48.477473],[9.70093,48.477573],[9.70293,48.477473],[9.70123,48.477473]]}

They work.
But i’m not quite sure about the limitations.

Best regards

PS: please open new topics for new questions

Thanks for the insight amandus!