Add Nutrition Data response - invalidArgument "Mismatched value count"

24 Views Asked by At

I am working with the Google Fit API to add nutrition data and following their Add Nutrition Data Guide.

I'm making a PATCH request with the following payload:

{
  "minStartTimeNs": 1624323600000000000,
  "maxEndTimeNs": 1624327200000000000,
  "dataSourceId": "my-datasource-id",
  "point": [
    {
      "startTimeNanos": 1624323600000000000,
      "endTimeNanos": 1624327200000000000,
      "dataTypeName": "com.google.nutrition",
      "value": [
        {
          "mapVal": [
            {
              "key": "calories",
              "value": {
                "fpVal": 456.7
              }
            },
            {
              "key": "fat.total",
              "value": {
                "fpVal": 20.7
              }
            },
            {
              "key": "fat.saturated",
              "value": {
                "fpVal": 11.3
              }
            },
            {
              "key": "fat.polyunsaturated",
              "value": {
                "fpVal": 0
              }
            },
            {
              "key": "fat.monounsaturated",
              "value": {
                "fpVal": 0
              }
            },
            {
              "key": "fat.trans",
              "value": {
                "fpVal": 0
              }
            },
            {
              "key": "cholesterol",
              "value": {
                "fpVal": 90
              }
            },
            {
              "key": "sodium",
              "value": {
                "fpVal": 753.3
              }
            },
            {
              "key": "potassium",
              "value": {
                "fpVal": 0
              }
            },
            {
              "key": "carbs.total",
              "value": {
                "fpVal": 32
              }
            },
            {
              "key": "dietary_fiber",
              "value": {
                "fpVal": 2.7
              }
            },
            {
              "key": "sugar",
              "value": {
                "fpVal": 2
              }
            },
            {
              "key": "protein",
              "value": {
                "fpVal": 30.7
              }
            }
          ]
        },
        {
          "intVal": 4
        }
      ]
    }
  ]
}

This returns a 400 error response with the following content:

{
  "error": {
    "code": 400,
    "message": "Mismatched value count: com.google.nutrition [2021-06-22T01:00:00Z - 2021-06-22T02:00:00Z] [{calories:456.7, fat.total:20.7, fat.saturated:11.3, fat.polyunsaturated:0.0, fat.monounsaturated:0.0, fat.trans:0.0, cholesterol:90.0, sodium:753.3, potassium:0.0, carbs.total:32.0, dietary_fiber:2.7, sugar:2.0, protein:30.7},4] raw:my-datasource-id",
    "errors": [
      {
        "message": "Mismatched value count: com.google.nutrition [2021-06-22T01:00:00Z - 2021-06-22T02:00:00Z] [{calories:456.7, fat.total:20.7, fat.saturated:11.3, fat.polyunsaturated:0.0, fat.monounsaturated:0.0, fat.trans:0.0, cholesterol:90.0, sodium:753.3, potassium:0.0, carbs.total:32.0, dietary_fiber:2.7, sugar:2.0, protein:30.7},4] raw:my-datasource-id",
        "domain": "global",
        "reason": "invalidArgument"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

What does this mean and how can I correct it? There does not seem to be any documentation I can find on this error.

1

There are 1 best solutions below

0
On

The food item needs to be included in the request body. In the guide, this is listed as:

{
    "strVal": "banana"
}

Including this makes the request succeed even though this value is not returned in the response and the Nutrition Data types page says:

either one, or both, of the meal type and food item fields are required