Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastApi Error : "RuntimeError: Response content longer than Content-Length" #4949

Closed
9 tasks done
sidraw-sys opened this issue May 25, 2022 · 14 comments
Closed
9 tasks done
Labels
answered bug Something isn't working reviewed

Comments

@sidraw-sys
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

#dummy list for posts
my_posts=[{"title":"Post 1","content":"Content of Post 1","id":1 },
          {"title":"Post 2","content":"Content of Post 2","id":2 },
          {"title":"Post 3","content":"Content of Post 3","id":3 }  
         ]

#function to get the index of the post which I am looking to delete by matching the ID
def find_index_post(id):
    for i, p in enumerate(my_posts):
        if p['id']==id:
            return i

#delete request created using FastApi
@app.delete("/posts/{id}",status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id:int):
    index=find_index_post(id)
    if index==None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Post with Id {id} does not 
        exist!")
    
    my_posts.pop(index)
    return Response(status_code=status.HTTP_404_NOT_FOUND)

Description

I have started learning FastApi recently and have been following one youtube tutorial for lessons.

The issue i have been facing with this code is whenever I run delete_post request on Postman I get below error on uvicorn server:
"return Response(status_code=status.HTTP_204_NO_CONTENT)
TypeError: Response.init() got an unexpected keyword argument 'status_code'"
However, in the instructors video, this is working fine without any errors.

I searched and found few issues related to this on here which I tried to follow but couldn't get myself to resolve the error. I tried following few solutions as provided inside below ISSUE ID but to no avail-
" Response content longer than Content-Length error for DELETE and NoContent #4939"

The two prominent errors I am getting are:

  • "return Response(status_code=status.HTTP_204_NO_CONTENT)
    TypeError: Response.init() got an unexpected keyword argument 'status_code'
    ***This is the original code which i am following from the youtube lesson

  • raise RuntimeError("Response content longer than Content-Length")
    RuntimeError: Response content longer than Content-Length
    ***This is the error i am getting when I try to follow the solution provided in Issue id "4939".

Kindly assist, Thanks!

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

Python 3.10.4

Additional Context

NA

@sidraw-sys sidraw-sys added the question Question or problem label May 25, 2022
@sidraw-sys
Copy link
Author

Also , on trying the solution as told to follow in #4939, i am getting an error i have mentioned at bottom:

///Updated Code for delete request:

@app.delete("/posts/{id}",status_code=status.HTTP_204_NO_CONTENT,response_class=Response)
def delete_post(id:int):
index=find_index_post(id)
if index==None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f"Post with Id {id} does not exist!")

my_posts.pop(index)
return None

///Getting below error:
response = actual_response_class(response_data, **response_args)
TypeError: Response.init() got an unexpected keyword argument 'background'

@sidraw-sys
Copy link
Author

sidraw-sys commented May 26, 2022

Hi Ben,
Sorry, but can you explain the resolution in bit detail, what modifications can I make in order to resolve the issue.
I went through the pull request but being a newbie I was not able to comprehend the tests and handling you did in there.
Thanks for your help, kindly assist in detail, if possible :)

@florimondmanca
Copy link

I believe this is a duplicate of #4939.

@tiangolo
Copy link
Owner

tiangolo commented Sep 3, 2022

Thanks @sidraw-sys! It seems this was handled in #5145, available since FastAPI 0.79.0.

If that solves it, you could close the issue. Thanks! 🍰

Thanks for the help @florimondmanca! 🙇

@tiangolo tiangolo added bug Something isn't working answered and removed question Question or problem labels Sep 3, 2022
@gazpachoking
Copy link

Commenting on this ticket as well, since it's still open.

This seems to be an issue for me still on fastapi 0.82.0. It can be reproduced like this:

from fastapi import FastAPI, HTTPException
import uvicorn

app = FastAPI()


@app.get("/")
async def root():
    raise HTTPException(204)

if __name__ == '__main__':
    uvicorn.run(app, debug=True)

Visiting the page results in:

RuntimeError: Response content longer than Content-Length

@github-actions github-actions bot removed the answered label Sep 8, 2022
@estronnom
Copy link

Stuck with the same problem here

@JarroVGIT
Copy link
Contributor

@gazpachoking and @estronnom what version of Uvicorn are you using?

@estronnom
Copy link

@JarroVGIT 0.18.3

@iudeen
Copy link
Contributor

iudeen commented Sep 8, 2022

Yes, I can confirm this seems to be an issue. I checked with pure starlette, its working fine there.

@gazpachoking
Copy link

@gazpachoking and @estronnom what version of Uvicorn are you using?

@JarroVGIT 0.18.3

Same. 0.18.3

@iudeen
Copy link
Contributor

iudeen commented Sep 8, 2022

I think I got the issue, will make a PR.

@iudeen
Copy link
Contributor

iudeen commented Sep 8, 2022

The reason this one keeps escaping the tests IMO is because FastAPI returns the given status code even if starlette raised 500 - Internal Server Error

@tiangolo
Copy link
Owner

Thanks everyone! This should have been fixed by @iudeen in #5365 🚀

This is available in the next version of FastAPI released in the next hours (the current is 0.82.0). 🎉

@github-actions
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered bug Something isn't working reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants