Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Approaches to Logging Food

I try use POST https://api.fitbit.com/1/user/[user-id]/foods/log.json

and I get {"errors":[{"errorType":"validation","fieldName":"date","message":"Date is required"}]}

 

I am sure the token work and successful set header for the request. Here is the code

// This part set the post parameters

JSONObject jsonObj = new JSONObject();
jsonObj.put("foodId", null);
jsonObj.put("foodName", name.getText().toString());
jsonObj.put("mealTypeId", 1);
jsonObj.put("unitId", 304);
jsonObj.put("amount", amount.getText().toString());
jsonObj.put("date", "2016-04-29");
jsonObj.put("favorite", false);
jsonObj.put("brandName", null);
jsonObj.put("calories", null);
new UploadLog().execute(jsonObj.toString());

 //This part set the header and set HTTP post

URL url = new URL(String.format(fitbit.loggingFoodRequest, fitbit.userID));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + fitbit.token);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

 //This part writes the parameters into the post request

Uri.Builder builder = new Uri.Builder().appendQueryParameter("json", params[0]);
String query = builder.build().getEncodedQuery();

OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();

 Any one can help me, Thanks

Best Answer
0 Votes
4 REPLIES 4

I already solve this problem, but any one try to use foodId to upload a food log.

Best Answer
0 Votes

Hi

 

Can  you please tell how you solve this issue because i am facing same (Date required ) issue.

 

Thanks

Sushil jindal

Best Answer
0 Votes

@Doug_MSExplorer Can I see the request you're making?

Andrew | Community Moderator, Fitbit

What motivates you?

Best Answer
0 Votes

I know this thread is old, but a Google search for " FitBit 'Date is required' " has only one applicable result (this one). I ran into this problem yesterday, since I just started using FitBit. Coming from a native developer background, using web APIs in code is new to me. What I found out is that you do not POST a JSON body with all the data you want to store. All of your data is posted with a query string.

 

If you are creating a JSON body like this...

{
    "foodLog":{
        "isFavorite":true,
        "logDate":"2011-06-29",
        "logId":21633,
        "loggedFood":{
            "accessLevel":"PUBLIC",
            "amount":2.55,
            "brand":"",
            "calories":370,
            "foodId":82294,
            "mealTypeId":7,
            "locale":"en_US",
            "name":"Chips",
            "unit":{"id":304,"name":"serving","plural":"servings"},
            "units":[304,226,180,147,389]
        },
        "nutritionalValues":{
            "calories":370,
            "carbs":47,
            "fat":17.5,
            "fiber":5,
            "protein":5,
            "sodium":325
        }
    }
}

...and post to

https://api.fitbit.com/1/user/-/foods/log.json

you will get the "Date is Required" error. Instead, you build the POST string like this:

 

https://api.fitbit.com/1/user/-/foods/log.json?date="2017-6-15"&_______

where _____ is the rest of your food log data. I hope this helps anyone else who comes across this post.

Best Answer
0 Votes