haudarren
haudarren

Reputation: 445

HERE Matrix Routing API - "Method not allowed for this action"

I am trying to get a reference example working, but the API throws a 405 error:

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix'
headers = {'Content-Type': 'application/json'}
payload = {
    "apiKey": <API_KEY>,
    "origins": [
        {"lat": 52.52103, "lng": 13.41268},
        {"lat": 52.51628, "lng": 13.37771},
        {"lat": 52.47342, "lng": 13.40357}
    ],
    "regionDefinition": {
        "type": "boundingBox",
        "north": 52.53,
        "south": 52.46,
        "west": 13.35,
        "east": 13.42
    },
    "matrixAttributes": ["distances"]
}

r = requests.get(url, headers=headers, params=payload)

print(json.dumps(r.json(), indent=2))

However, this gives

{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}

My r.url is https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&origins=lat&origins=lng&origins=lat&origins=lng&origins=lat&origins=lng&regionDefinition=type&regionDefinition=north&regionDefinition=south&regionDefinition=west&regionDefinition=east&matrixAttributes=distances

This obviously looks wrong, so I tried modifying my code to

r = requests.get(url, headers=headers, params=json.dumps(payload))

This yields an r.url of https://matrix.router.hereapi.com/v8/matrix?%7B%22apiKey%22:%20%22API_KEY%22,%20%22origins%22:%20[%7B%22lat%22:%2052.52103,%20%22lng%22:%2013.41268%7D,%20%7B%22lat%22:%2052.51628,%20%22lng%22:%2013.37771%7D,%20%7B%22lat%22:%2052.47342,%20%22lng%22:%2013.40357%7D],%20%22regionDefinition%22:%20%7B%22type%22:%20%22boundingBox%22,%20%22north%22:%2052.53,%20%22south%22:%2052.46,%20%22west%22:%2013.35,%20%22east%22:%2013.42%7D,%20%22matrixAttributes%22:%20[%22distances%22]%7D

This also doesn't look right, so I tried the simplest example

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false&regionDefinition=world&profile=truckFast'
headers = {'Content-Type': 'application/json'}

r = requests.get(url, headers=headers)

print(json.dumps(r.json(), indent=2))

This yields an r.url of https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false&regionDefinition=world&profile=truckFast

In all of these examples, the API still returns

{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}

Why is this happening?

In case it matters, my requests.__version__ is 2.27.1.

Upvotes: 0

Views: 476

Answers (2)

haudarren
haudarren

Reputation: 445

Looks like the issue was indeed using GET rather than POST. However, posting a more complete answer with code examples, to resolve the questions around how to set the payload, etc.

import requests
import json

url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=<API_KEY>&async=false'
headers = {'Content-Type': 'application/json'}

payload = {
    "origins": [
        {"lat": 52.52103, "lng": 13.41268},
        {"lat": 52.51628, "lng": 13.37771},
        {"lat": 52.47342, "lng": 13.40357}
    ],
    "regionDefinition": {
        "type": "boundingBox",
        "north": 52.53,
        "south": 52.46,
        "west": 13.35,
        "east": 13.42
    },
    "matrixAttributes": ["distances"]
}

r = requests.post(url, headers=headers, data=json.dumps(payload))

print(json.dumps(r.json(), indent=2))

This results in

{
  "matrixId": "fdaa07ea-f8d9-4b3f-a410-9c3b2d54e464",
  "matrix": {
    "numOrigins": 3,
    "numDestinations": 3,
    "distances": [
      0,
      2924,
      7258,
      3221,
      0,
      7710,
      7333,
      7721,
      0
    ]
  },
  "regionDefinition": {
    "type": "boundingBox",
    "west": 13.35,
    "south": 52.46,
    "east": 13.42,
    "north": 52.53
  }
}

Upvotes: 0

user3505695
user3505695

Reputation:

Submit matrix for route calculation using POST method:

https://matrix.router.hereapi.com/v8/matrix?regionDefinition=world&profile=truckFast&apiKey=

Also please refer to Matrix routing API Examples

Upvotes: 1

Related Questions