0

I'm trying to test TomTom's API Matrix Routing v2 -> Synchronous Matrix (documentation here) with Python's requests.

Here is my code, using their example from the documentation:

import requests

apiKey      = 'add_api_key_here'
apiURL      = f'https://api.tomtom.com/routing/matrix/2?key={apiKey}'

post_params = {
                  "origins": [
                    {
                      "point": {"latitude": 36.98844, "longitude": -121.97482}
                    }
                  ],
                  "destinations": [
                    {
                      "point": {"latitude": 34.13007, "longitude": -118.228610}
                    }
                  ],
                  "options": {
                    "traffic": "historical",
                    "travelMode": "truck",
                    "vehicleCommercial": True
                  }
              }

headers = {'Content-Type' : 'application/json' }
resp = requests.post(apiURL, data=post_params, headers=headers)

Why am I getting the Response [400] error, saying:

{"detailedError":{"code":"BAD_REQUEST","message":"Bad Request","details":[{"code":"MALFORMED_BODY","message":"Could not parse matrix request","target":"postBody"}]}}

There is surely something I'm missing, I simply cannot see it. :(

Sandy B
  • 35
  • 4

1 Answers1

1

Nevermind, I found my own error 2 minutes later: replace data=post_params with json=post_params inside the requests.post. :)

Sandy B
  • 35
  • 4
  • Hi, self-answering a question is [good practice](https://stackoverflow.com/help/self-answer), but the answer would be even more useful with some [more details](https://stackoverflow.com/help/how-to-answer). – totokaka Oct 05 '21 at 16:03