question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing a response with a list as a return to the fastapi the fastapi returns a strange json {"91":93} with 200 status

See original GitHub issue

Description

By passing a response that return a empty list the fastapi returns this istrange json ‘{“91”:93}’. the situation can be reproduced by using the following code with fastapi

How to reproduce the problem

https://github.com/LeonardoLeano333/fastapi_example/blob/18925e2e7684d6a399fc6f59d6dabf77d35f2983/app/main.py my code: <main.py>

import requests
from app.src.sth_func import asd
from fastapi import FastAPI

app = FastAPI()

@app.post("/")
async def root():
    return []

@app.get("/up")
async def root_b():
    response = requests.post("http://localhost:8000/")
    
    return response

running two servers:

for the python request: uvicorn app.main:app --port 8081

for the inner response in the endpoint: uvicorn app.main:app --port 8000

the request:

>>> import requests
>>> asd = requests.get("http://localhost:8081/up/")
>>> asd
<Response [200]>
>>> asd.text
'{"91":93}'

Questions

I guess in this situation it should return a 500 or something like this but not a json that do not means much.

My question is why this is like this? Or what it should be if this answer fron the fast api is not correct?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ComicShrimpcommented, Jul 22, 2021

I think that this problem is not related to the framework, is more related to programming itself. If the pieces work together, it works. In this case is more like a semantic error, where all the pieces work together, but don’t produce the result you want, and not a syntax error where the pieces don’t work together and it should return an error. This is my thought about this error.

Another thing that a founded in the example code, the functions have the same name, this could be causing the problems. You can try something like this:



import requests
from app.src.sth_func import asd
from fastapi import FastAPI

app = FastAPI()

@app.post("/")
async def root():
    return []

@app.get("/up")
async def root_2():
    response = requests.post("http://localhost:8000/")
    
    return response.json()
0reactions
github-actions[bot]commented, Dec 8, 2022

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Response Status Code - FastAPI
Return that status code in the response. Document it as such in the OpenAPI schema (and so, in the user interfaces):. Note. Some...
Read more >
FastAPI always returns content, even if 204 no content status ...
FastAPI does not always return content, as stated in the issue title, it just does it by default. The default JSONResponse , which...
Read more >
Fast API : How to return a str as JSON - python - Stack Overflow
I investigated as it was strange that the Fast API example didn't work. The problem was that I was not using the right...
Read more >
tiangolo/fastapi - Gitter
@router.get("/", response_model=List[UserOut]) async def users_list(): ... you could return a JSONResponse with the {"status": "failed"} in place of the ...
Read more >
Python FastAPI crash course: Make your first CRUD API
Tagged with python, fastapi, beginners. ... found the path will return "Not found" however the status code returned will always be "200 OK"....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found