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

Log weight using OkHTTP gives 400 Bad Request

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"}]}

Best Answer
0 Votes
7 REPLIES 7

Perhaps 'Weight' should be 'weight'?

Best Answer
0 Votes
Tried that but makes no difference. Do you know how the api wants the data? Urlencoded or is json format okay?
Best Answer
0 Votes

That's it. All POST requests to the Fitbit Web API should be as body parameters.

Best Answer
0 Votes

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?

Best Answer
0 Votes

@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.

Best Answer
0 Votes

Did you ever figure this out? I am running into the same problem here. 

Best Answer
0 Votes

Hi @alihashemi

 

The parameter names are case-sensitive.  Make certain they are all lower-case.

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes