i am sending a post request to
https://routes.googleapis.com/directions/v2:computeRoutes?key=APIKEY
with body as
{
    "origin": {
        "location": {
            "latLng": {
                "latitude": 37.7749,
                "longitude": -122.4194
            }
        }
    },
    "destination": {
        "location": {
            "latLng": {
                "latitude": 34.0522,
                "longitude": -118.2437
            }
        }
    },
    "travelMode": "DRIVE",
    "routingPreference": "TRAFFIC_UNAWARE",
    "polylineQuality": "HIGH_QUALITY",
    "polylineEncoding": "POLYLINE_ENCODING_UNSPECIFIED",
    // "departureTime": "2023-07-25T12:00:00",
    // "arrivalTime": "2023-07-25T18:00:00",
    "computeAlternativeRoutes": true,
    "languageCode": "en",
    "regionCode": "US",
    "units": "IMPERIAL",
    "optimizeWaypointOrder": true,
    "extraComputations": [
        "FUEL_CONSUMPTION"
    ],
    "trafficModel": "BEST_GUESS",
    "transitPreferences": {
        "allowedTravelModes": [
            "BUS",
            "SUBWAY"
        ]
    }
}
and headers as
key: X-Goog-FieldMask
value:  routes.distanceMeters,routes.duration
reference: https://developers.google.com/maps/documentation/routes/choose_fields#specify-field
i am getting response as
{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
    }
}
if i dont include my X-Goog-FieldMask header or make it blank .
then the response is
{
    "error": {
        "code": 400,
        "message": "FieldMask is a required parameter. See https://cloud.google.com/apis/docs/system-parameters on how to provide it. As an example, you can set the header 'X-Goog-FieldMask' to value 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline' to ask for the route distance, duration, and polyline in the response. You can also set the value to '*' in manual testing to get all the available response fields. However, using the '*' wildcard is discouraged in production.'",
        "status": "INVALID_ARGUMENT"
    }
}
even though in the docs it says
https://cloud.google.com/apis/docs/system-parameters
if i set the X-Goog-FieldMask as something which is absolutely wrong. like
key: X-Goog-FieldMask
value: routes.something
then i get the response which is expected
{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "routes.something",
                        "description": "Error expanding 'fields' parameter. Cannot find matching fields for path 'routes.something'."
                    }
                ]
            }
        ]
    }
}
i am using postman to send the post request.
i also tried this in curl an the result was same.
I don't know if you solved this but I found this question while trying to solve something different, and I thought I'd answer it.
In your example, you're asking to optimizeWaypointOrder but you offer no intermediates. This will give you that error.
You are also including a trafficModel in your request, but you set the routingPreference to TRAFFIC_UNAWARE, which is conflicting and will also give you that error.
So, you need to:
routingPreference=TRAFFIC_AWARE_OPTIMAL in order to use trafficModeloptimizeWaypointOrderBy making those changes the request was successful when I tested it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With