Google Routes api X-Goog-FieldMask is not working

667 views Asked by At

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 enter image description here 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.

3

There are 3 answers

0
Joseph Sabido On BEST ANSWER

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:

  • Use routingPreference=TRAFFIC_AWARE_OPTIMAL in order to use trafficModel
  • Remove optimizeWaypointOrder

By making those changes the request was successful when I tested it.

0
Gavin On

I was having a similar issue to this where I would get a successful response with a wildcard(*) in the X-Goog-FieldMask header, but would get a 400 error with no details like your example when I tried to specify the 2 fields I was interested in.

I ended up getting a successful response after removing the polylineQuality and polylineEncoding fields in my request.

I am making my requests from Power Query with Web.Contents().

Unsuccessful Request Body:

[
    origin = [From.Waypoint],
    destination = [To.Waypoint],
    travelMode = "DRIVE",
    routingPreference = "TRAFFIC_UNAWARE",
    polylineQuality = "OVERVIEW",
    polylineEncoding = "ENCODED_POLYLINE",
    computeAlternativeRoutes = false,
    languageCode = "en-US",
    regionCode = "ca",
    units = "METRIC"
]

Headers = 
          [
          #"Content-Type" = "application/json",
          #"X-Goog-Api-Key" = "APIKEY",
          #"X-Goog-FieldMask" = "routes.duration"
          ]

Successful Request Body:

[
    origin = [From.Waypoint],
    destination = [To.Waypoint],
    travelMode = "DRIVE",
    routingPreference = "TRAFFIC_UNAWARE",
    computeAlternativeRoutes = false,
    languageCode = "en-US",
    regionCode = "ca",
    units = "METRIC"
]

Headers = 
          [
          #"Content-Type" = "application/json",
          #"X-Goog-Api-Key" = "APIKEY",
          #"X-Goog-FieldMask" = "routes.duration"
          ]

It's not quite a solution as I haven't pinpointed why I can't specify the polyline fields while also specifying a FieldMask, but might help to start with the required fields and add the optional ones one-by-one.

0
Mohamed Ali Ben Thaier On

After several hours trying to fix the issue for me, it was that I needed to add routes.optimized_intermediate_waypoint_index to the mask (of course I didn't see this error message since I am using a serverless function and the logs there didn't provide much info until I curl requested directly to the Routes API)

{
  "error": {
    "code": 400,
    "message": "Requests with optimize_waypoint_order set to True also need to request for routes.optimized_intermediate_waypoint_index in the fieldmask.",
    "status": "INVALID_ARGUMENT"
  }
}