API Login Call Not Working As Expected

I am trying to authenticate with the API using Python and Curl.

Python

requests.post(‘address/api/v1/login’,data={“username”:“user”,“password”:“pass”})
Response: {‘code’: 1004, ‘message’: ‘Please specify a username and a password.’}

Curl

curl -i -X POST -H ‘Content-Type: application/json’ -d {“password”:“pass”,“username”:“user”,} address/api/v1/login
Response:
curl: (3) URL using bad/illegal format or missing URL
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=UTF-8
Vary: Origin
Date: Date
Content-Length: 54

{“message”:“Please provide a username and password.”}


However, when I grab the JWT token from when I login, the API appears to work. Any idea what is going on with the login request?

With curl, this request works for me:

curl https://try.vikunja.io/api/v1/login -d '{"username":"demo","password":"demo"}' -H 'Content-Type: application/json'

You have to provide the login parameters as a single json string. In your curl example it looks like you’re providing them without ' which will likely result in your shell parsing the json object and something else getting transferred by curl. Also you have trailing comma at the end of the string which makes the json invalid.
Additionally you will need the Content-Type header but that part looks correct in your curl example.

I don’t know any python, but I think in your python code you’re passing the login data as form params, not a json string. And it looks like the header is missing.

Thank you! Very simple fix!

For future Python users, you do not need to add the Content-Type header when you post with the Requests library (or at least not at this time). One of the following should work.

requests.post(‘address/api/v1/login’,json={“username”:“user”,“password”:“pass”})
or
requests.post(‘address/api/v1/login’,data=json.dumps({“username”:“user”,“password”:“pass”}),headers={‘Content-Type’: ‘application/json’})