Anvil HTTP Error StreamingMedia

The anvil http requests library seems to behave quite different to the normal python requests package.
for example:
import json
import anvil.http
import anvil.server

 data = requests.get(url)
 result = data.json().get('result')

works perfectly fine, but

 data = anvil.http.request(url)
 result = data.json().get('result')

logs the following error: AnvilWrappedError: ‘StreamingMedia’ object has no attribute ‘json’
While

 data = anvil.http.request(url,json=True) 

returns a dict not a json which is confusing but manageable.

But am getting similar issues while trying to create an endpoint that recieves json. anvil.server.body will return a plain text of the request (not in json), but anvil.server.body_json returns None.
When trying to perform methods on anvil.server.body, i get the same AnvilWrappedError: ‘StreamingMedia’ object has no attribute ‘json’ or while using it as a dict ‘StreamingMedia’ object is not subscriptable.

What is this StreamingMedia object and how can i perform typical methods and operations with anvil?

If I remember correctly, this same issue came up here:

thanks, any info regarding the endpoints?

Here are the docs on endpoints.

https://anvil.works/docs/http-apis/creating-http-endpoints

ive already read through those and no help.
I need to receive a json post request.

anvil.server.body_json returns None
and anvil.server.body returns StreamingmMedia errors.
this returns none
@anvil.server.http_endpoint(’/testendpoint’,methods=‘POST’)
def test():
data = anvil.server.request.body_json

this returns a bizzare plain text media object and cannot perform any operations/methods on it:
@anvil.server.http_endpoint(’/testendpoint’,methods=‘POST’)
def test():
data = anvil.server.request.body

data.content_type gives either plain/text utc-8 when data=anvil.server.body, or
data.content_type gives AttributeError: ‘NoneType’ object has no attribute ‘content_type’ when data = anvil.server.body_json

If the plain text is in JSON format, then, once you extract the text, you should be able to use standard Python library routines to convert it into the in-Python equivalent: e.g., a list or dict.

At least, as a workaround, until the real problem is found.

1 Like

Hi @bdr997,

anvil.server.request.body_json should be what you’re after. The following is working for me:

Server Module code:

import anvil.server
import anvil.http

@anvil.server.http_endpoint('/testendpoint', methods=['POST'])
def test():
  body_json = anvil.server.request.body_json
  print(f"Body json: {body_json}")
  print(f"Content type: {anvil.server.request.body.content_type}")
  return anvil.server.HttpResponse(200, "Request received")

Json POST request:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  https://artistic-downright-contest.anvil.app/_/api/testendpoint

Looking at the app logs, I get the following output:

Incoming API request from Cambridge, United Kingdom.
Body json: {'username': 'xyz', 'password': 'xyz'}
Content type: application/json
3 Likes