HTTP Endpoints in POST

Hi all
I have a HTTP endpoint defined like this:

@anvil.server.http_endpoint("/servercallback")
def server_callback(**params):
  print('QUERYSTRING PARAMS')
  print(params)
  form_params = anvil.server.request.form_params
  print('FORM PARAMS')
  print(form_params)  
  body = anvil.server.request.body
  print('REQ BODY')
  print(body)  
  json = anvil.server.request.body_json
  print('REQ BODY JSON')
  print(json)  
  print('ALL REQUEST PROPERTIES:')
  print('Headers')
  print(anvil.server.request.headers)  
  print('Method')
  print(anvil.server.request.method)  
  print('Origin')
  print(anvil.server.request.origin)  
  print('Password')
  print(anvil.server.request.password)  
  print('Path')
  print(anvil.server.request.path)  
  print('Query Params')
  print(anvil.server.request.query_params)  
  print('Remote Address')
  print(anvil.server.request.remote_address)  
  print('Username')
  print(anvil.server.request.username)  

This way I log everything to my APP log for testing purposes.

I call this with POSTMAN method POST and pass parameters (testing purposes) both as querystring and form_data:


What I get in the APP Log is:

QUERYSTRING PARAMS
{'query_param': 'querystring'}
FORM PARAMS
{}
REQ BODY
<anvil._serialise.StreamingMedia object at 0x0000000001a3c918>
REQ BODY JSON
None
ALL REQUEST PROPERTIES:
Headers
{'host': 'platform-server:3000', 'user-agent': 'PostmanRuntime/7.24.1', 'content-type': 'multipart/form-data; boundary=--------------------------315042450429081305573792', 'cookie': None, 'content-length': '174', 'accept': '*/*', 'ssl-client-verify': 'NONE', 'x-forwarded-for': '79.19.65.234', 'accept-encoding': 'gzip, deflate, br', 'postman-token': 'ab247f0b-deb0-44bb-9cc6-446a7bc47675', 'x-real-ip': '79.19.65.234', 'cache-control': 'no-cache', 'anvil-host': 'ry2cnmkusvykd3fw.anvil.app'}
Method
POST
Origin
None
Password
None
Path
/servercallback
Query Params
{'query_param': 'querystring'}
Remote Address
79.19.65.234
Username
None

Why can’t I see body_param and its value?

I need to read formdata because my endpoint is called by an external application that passes data that way, using POST method.

I feel I’m missing something simple, but I’ve searched the forum and the docs and can’t really find a clue.

Thanks and BR

1 Like

the body is a media object. Have you tried calling get_bytes() on anvil.server.request.body, like:

anvil.server.request.body.get_bytes()
2 Likes

What content type does the external application use? In your screenshot and corresponding log, it’s set to “multipart/form-data”. For simple name and value pairs, the content type is typically “application/x-www-form-urlencoded”

Regards

1 Like

Anvil tries to setup the parameters for you, but too many things can go wrong, so I always do, as @stucork suggests, use anvil.server.request.body.get_bytes() like this:

@anvil.server.http_endpoint('/endpoint_name', methods='POST')
def endpoint_name():
  bb = anvil.server.request.body.get_bytes()
  data = json.loads(bb)
  par1 = data['par1']
  par2 = data['par2']
4 Likes

Thanks all,
actually after the first hint from Stu I was able to work it out.
Yes @ahawes the content-type was “application/x-www-form-urlencoded” as I could see as soon as I got the bytes from the body. :smiley: