Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API habit completion returns error #55

Closed
frankrehfeld opened this issue Jan 1, 2025 · 5 comments
Closed

API habit completion returns error #55

frankrehfeld opened this issue Jan 1, 2025 · 5 comments

Comments

@frankrehfeld
Copy link

When trying to complete a habit via API it returns an error.

curl --request POST --url http://192.168.1.60:8081/api/v1/habits/fc901c/completions \
 --header 'accept: application/json' \
 --header 'authorization: Bearer xxx' \
 --data '{"done": true, "date_fmt": "%d-%m-%Y", "date": "01-01-2025"}

returns the following error message:

{"detail":[{"type":"model_attributes_type","loc":["body"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"{\"done\": true, \"date_fmt\": \"%d-%m-%Y\", \"date\": \"01-01-2025\"}"}]}
@daya0576
Copy link
Owner

daya0576 commented Jan 2, 2025

Thanks for reaching out. It seems like the ' is missing at the end of your request, could you correct it and try again?

@frankrehfeld
Copy link
Author

Hi. The ' was lost when copying the request into the issue. The request was done with the trailing ' and returned the error. I've just controlled it and got the same behaviour.

@daya0576
Copy link
Owner

daya0576 commented Jan 3, 2025

OK, could you add the content-type header to your request, here is an example.

@frankrehfeld
Copy link
Author

Thanks for your help. It worked as expected.

FYI: I used it to build a switch for home assistant to mark a habit as done. This can be achieved by implementing a rest switch using the following code:

- platform: rest
  resource: http://192.168.1.60:8081/api/v1/habits/fc901c/completions
  params:
    date_start: '{{ now().strftime("%d-%m-%Y") }}'
    date_end: '{{ now().strftime("%d-%m-%Y") }}'
    sort: asc
  headers:
    content-type: application/json
    accept: application/json
    authorization: Bearer xxx
  is_on_template: '{{ now().strftime("%d-%m-%Y") in value_json }}'
  body_on: '{"done": true, "date": "{{ now().strftime("%d-%m-%Y") }}" }'
  body_off: '{"done": false, "date": "{{ now().strftime("%d-%m-%Y") }}" }'
  name: medicine
  icon: mdi:pill

Maybe you are interested in adding it in the documentation.

@daya0576
Copy link
Owner

daya0576 commented Jan 3, 2025

Thanks for sharing, I'm a big fan of HA too :)

That integration looks pretty cool, I have added it to the readme homepage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants