What I’m trying to do:
I have an API hosted on AWS that I want to call from Anvil. For starters, I simply need to login. When I try this locally with the built-in python requests module, it works just fine like this:
import requests
data = {'username': 'abc', 'password': '123'}
r = requests.post(BASE_URL + 'auth/jwt/login', data=data)
However, when I try to replicate this from Anvil (client side module), the following code stopped working:
import anvil.http
data = {'username': 'abc', 'password': '123'}
r = anvil.http.request(BASE_URL + 'auth/jwt/login', method='POST', data=data)
More precisely, I’m getting a 422 Unprocessable Entity error:
{"detail":[{"loc":["body","username"],"msg":"field required","type":"value_error.missing"},{"loc":["body","password"],"msg":"field required","type":"value_error.missing"}]}
What I’ve tried and what’s not working:
I’ve tried all 4 combinations of adding “json=True” and manually converting my data to json with “data = json.dumps(data)” before sending it, but to no avail.
I’ve also investigated further via Chrome’s DevTools and curlconverter.com and it seems I can’t replicate the triggering of the same curl than with python’s request module (first code snippet).
With “json=False”, Anvil does the equivalent of:
import requests
[...]
data = '[object Object]'
response = requests.post(BASE_URL + 'auth/jwt/login', data=data)
With “json=True”, Anvil does the equivalent of:
import requests
[...]
json_data = {
'username': 'abc',
'password': '123',
}
response = requests.post(BASE_URL + 'auth/jwt/login', json=json_data)
With “data = json.dumps(data)” before sending it, Anvil does the equivalent of:
import requests
[...]
data = '{"username":"abc","password":"123"}'
response = requests.post(BASE_URL + 'auth/jwt/login', data=data)
(or the same with json=json_data instead of data=data if in case I set “json=True”)
In all cases, I get the same 422 error. I’ve also tried adding different headers including ‘accept’: ‘application/json’, ‘Content-Type’: ‘application/json’, and ‘Content-Type’: ‘application/x-www-form-urlencoded’.
I’m guessing I’m missing something very basic here. What could it be?