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

400 bad request when I POST method with python

I just started to develop with Fitbit devices.

I want to post Alarm to my devices ,so I wrote a code.
Here is the code

import json
import requests

headers = {
	"Authorization": "Bearer secret",
	"content-type": "application/json"
}
payload={"time":"09:00-09:15","Enabled":'true',"recurring":True,"weekDays":"MONDAY"}
url = '<a href="https://api.fitbit.com/1/user/-/devices/tracker/example/alarms.json" target="_blank">https://api.fitbit.com/1/user/-/devices/tracker/example/alarms.json</a>'

r=requests.post(url,data=json.dumps(payload),headers=headers)

but it returns

{"errors":[{"errorType":"validation","fieldName":"recurring","message":"Recurring is required"},{"errorType":"validation","fieldName":"time","message":"Time is required"},{"errorType":"validation","fieldName":"enabled","message":"Enabled is required"}]}

 

I can't fix it.How do I to fix it?

Best Answer
0 Votes
4 REPLIES 4

Hi @takepan ,

 

I'm not that familiar with python.  Here are a couple of things to consider

1. the "devices" scope should be enabled

2. you can add alarms for trackers, not smart watches (Ionic and Versa family)

3. check the case of the parameter name and the value.  I think they should be all camel case.

 

The URL should look something like this

 

POST "https://api.fitbit.com/1/user/-/devices/tracker/755971717/alarms.json?time=09%3A00-09%3A15&enabled=t..."

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

It worked.Thanks.

But I have another question. How to specify multiple days of the week?

Best Answer
0 Votes

Sorry but it doesn't work. It returns 500 error.

{"errors":[{"errorType":"request","fieldName":"500","message":"An error occurred with the Fitbit Web API while processing the request. Error id: A29E072D:B50A_A92D87A6:01BB_5CB13337_4E1B7C7C:3E55"}],"success":false}

How do I fix it? 

Best Answer
0 Votes

To enabled the alarm for multiple days, you need to do 2 things

1. parameter "recurring=true"

2. parameter "weekDays" list the days of the week in all capital letters separated by comma, no spaces.  

 

Here's an example

POST https://api.fitbit.com/1/user/-/devices/tracker/802775263/alarms.json?time=07:00-08:00&enabled=true&...

 

We have this information documented here: https://dev.fitbit.com/build/reference/web-api/devices/#add-alarm

 

If you're still having a problem, please include the URL you are executing.

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