12-01-2015 00:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-01-2015 00:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I am upgrading my existing code using the Android OkHTTP library. I sucessfully can do GET requests but somehow I do not manage to POST data to the server.
For Logging weight I get the error {"errors":[{"errorType":"validation","fieldName":"weight","message":"Weight is required"}]}
So i know that i do not post the Weight value but I think i do!!
Here is my code and the error message below. It must be something obvious but I seem not to see it!
==========================================================
JSONObject jsonObject = new JSONObject();
jsonObject.put("Weight", “88.6”);
jsonObject.put("date", "2015-12-01");
jsonObject.put("time", "19:06");
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString());
Request request = new Request.Builder()
.url("https://api.fitbit.com/1/user/-/body/log/weight.json")
.header("Authorization", "Bearer " + “my valid accesstoken here”)
.addHeader("Accept-Language", "en_GB")
.post(body)
.build();
Log.e(Constants.TAG, "REQUEST: " + bodyToString(request));
Response response = mOkHttpClient.newCall(request).execute();
=========================================================
STACKTRACE. I printed the headers. As you can see in the request there is the "Weight" parameter...
https://api.fitbit.com/1/user/-/body/log/weight.json
REQUEST: {"Weight":"88.6","date":"2015-12-01","time":"19:06"}
OkHttp-Selected-Protocol: spdy/3.1
server: cloudflare-nginx
date: Tue, 01 Dec 2015 08:06:19 GMT
content-type: application/json;charset=UTF-8
set-cookie: __cfduid=d44ff07c770f50c396baac34ec5bff53e1448957179; expires=Wed, 30-Nov-16 08:06:19 GMT; path=/; domain=.fitbit.com; HttpOnly
set-cookie: JSESSIONID=E3E559611549B296C510CBFA7D15CE8D.fitbit1; Path=/; HttpOnly
x-ua-compatible: IE=edge,chrome=1
expires: Thu, 01 Jan 1970 00:00:00 GMT
cache-control: no-cache, must-revalidate
pragma: no-cache
fitbit-rate-limit-limit: 150
fitbit-rate-limit-remaining: 148
fitbit-rate-limit-reset: 3221
content-language: en-GB
.vary: Accept-Encoding
x-frame-options: SAMEORIGIN
cf-ray: 24dd4ac1b7c40b0e-SYD
OkHttp-Sent-Millis: 1448957179165
OkHttp-Received-Millis: 1448957179979
{"errors":[{"errorType":"validation","fieldName":"weight","message":"Weight is required"}]}

12-01-2015 11:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



12-01-2015 11:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Perhaps 'Weight' should be 'weight'?

12-01-2015 12:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-01-2015 12:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-01-2015 12:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



12-01-2015 12:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
That's it. All POST requests to the Fitbit Web API should be as body parameters.

12-01-2015 15:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-01-2015 15:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Ok got it working with the following code. The weird thing is that i tried this in a first instance but it gave a 400 Bad request error. Not sure what happened there...
RequestBody formBody = new FormEncodingBuilder()
.add("weight", "88.6")
.add("date", "2015-12-02")
.add("time", "10:38:44") // NOTE be sure you use this format HH:mm:ss
.build();
Request request = new Request.Builder()
.url(url)
.header("Authorization", "Bearer " + "my_access_token")
.addHeader("Accept-Language", "en_GB")
.post(formBody)
.build();
Response response = mOkHttpClient.newCall(request).execute();
The resonse is then:
Jsonobject: {
"weightLog": {
"bmi": 157.51,
"date": "2015-12-02",
"logId": 1449052724000,
"time": "10:38:44",
"weight": "2015-12-02"
}
}
NOTE: the doc says as example response {weight: etc...} but it is weightLog. Maybe Fitbit can update their documentation?

12-01-2015 15:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
12-01-2015 15:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@myfitnesscomp wrote:
NOTE: the doc says as example response {weight: etc...} but it is weightLog. Maybe Fitbit can update their documentation?
Thank you for reporting this, we will have it fixed.

11-20-2018 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

11-20-2018 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Did you ever figure this out? I am running into the same problem here.

11-21-2018 14:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



11-21-2018 14:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @alihashemi
The parameter names are case-sensitive. Make certain they are all lower-case.
Gordon
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

