import httpclient, json
# Build the JSON request body
let data = %*{
"token": {
"user": user,
"password": passwd,
"lifetime": "fixed"
}
}
# --- BROKEN: curly library does not send body correctly ---
# curl from terminal works, httpclient works too, but curly does not.
# curly likely ignores/drops the body on POST requests.
#
# How to reproduce:
# var headers = emptyHttpHeaders()
# headers["Accept"] = "application/json"
# headers["Content-Type"] = "application/json"
# let resp = curl.post("https://api.vpsfree.cz/_auth/token/tokens", headers, body = $data)
# -> returns 422/400, server never receives the body
#
# Equivalent curl command that works:
# curl -X POST https://api.vpsfree.cz/_auth/token/tokens \
# -H 'Content-Type: application/json' \
# -d '<data>'
# --- WORKS: standard httpclient ---
let client = newHttpClient()
client.headers = newHttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json"
})
let resp = client.post("https://api.vpsfree.cz/_auth/token/tokens", body = $data)
echo resp.body