Make authenticated request from python rest client

Im trying to make a POST request to a better-auth endpoint and even thjo the cookie is set the session/user is not being populated. Example code:
import requests

loginRes = requests.post(
"http://localhost:4321/api/auth/sign-in/username",
data={"username": "", "password": ""},
)
loginData = loginRes.json()
token = loginData["token"]

cookies = {"better-auth.session_token": token}
data = {"title": "Test Title"}
createRes = requests.post(
"http://localhost:4321/api/album/create", data=data, cookies=cookies
)

print(createRes)
import requests

loginRes = requests.post(
"http://localhost:4321/api/auth/sign-in/username",
data={"username": "", "password": ""},
)
loginData = loginRes.json()
token = loginData["token"]

cookies = {"better-auth.session_token": token}
data = {"title": "Test Title"}
createRes = requests.post(
"http://localhost:4321/api/album/create", data=data, cookies=cookies
)

print(createRes)
4 Replies
bekacru
bekacru2mo ago
use bearer plugin instead
Chaito
ChaitoOP2mo ago
Oh I thought that was discouraged Ok! What's the technical reason it doesn't work with cookies
bekacru
bekacru2mo ago
possible reason - the token isn't signed - the server is running in prod and is expecting a __Secure prefixed cookie
Chaito
ChaitoOP2mo ago
Aaah ok, thanks!

Did you find this page helpful?