I've authenticated fine, retrieved my token and am able to query data without problem. However, when attempting to POST data, I keep getting the error message:
{"errors":[{"errorType":"validation","fieldName":"date","message":"Date is required"}]}
The POST payload is as per the documentation for logging by foodId:
{"foodId": 81911,
"date": "2019-10-13",
"amount": 2,
"unitId": 226,
"mealTypeId": 7,
"favorite": false}
As you can see, there's definitely a date there! So why the error message?
I'm trying to POST to https://api.fitbit.com/1/user/-/foods/log.json as per the docs, with the token in the headers. (Repeat: I can retrieve data fine so the headers/token is correct).
Is everybody else able to log foods via the Web API ? What am I missing here please?
Thank you,
C.
Answered! Go to the Best Answer.
OK, have solved this. Really simple error. Turns out that the Fitbit Web API POST method does not support a JSON payload but rather expects to receive form data.
So instead of this:
curl -X POST \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9xxxxxx" " \
--data '{"foodId": 81911, "date": 2019-10-13, "amount": 2, "unitId": 226, "mealTypeId": 7, "favorite": false}' \
https://api.fitbit.com/1/user/-/foods/log.json
We have to use this:
curl -X POST \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9xxxxxx" \
--data "foodId=81911&date=2019-10-13&amount=2&unitId=226&mealTypeId=7&favorite=false" \
https://api.fitbit.com/1/user/-/foods/log.json
Given that the URLs of the Fitbit API endpoints end in ".json", this seems incredibly non-intuitive.
Also, the error messages were misleading. They should have mentioned that the format of data was incorrect, that they did not receive form data.
C.
OK, have solved this. Really simple error. Turns out that the Fitbit Web API POST method does not support a JSON payload but rather expects to receive form data.
So instead of this:
curl -X POST \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9xxxxxx" " \
--data '{"foodId": 81911, "date": 2019-10-13, "amount": 2, "unitId": 226, "mealTypeId": 7, "favorite": false}' \
https://api.fitbit.com/1/user/-/foods/log.json
We have to use this:
curl -X POST \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9xxxxxx" \
--data "foodId=81911&date=2019-10-13&amount=2&unitId=226&mealTypeId=7&favorite=false" \
https://api.fitbit.com/1/user/-/foods/log.json
Given that the URLs of the Fitbit API endpoints end in ".json", this seems incredibly non-intuitive.
Also, the error messages were misleading. They should have mentioned that the format of data was incorrect, that they did not receive form data.
C.