質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Pythonista

Pythonistaは、iOS上でPythonプログラミングができる開発アプリです。さらに、Pythonの関数・変数などを自動で補完する便利なコードエディタや、PythonスクリプトをiOS上で多様な形で機能させる各種機能も内包しています。

Q&A

解決済

1回答

1751閲覧

h11._util.LocalProtocolError: Too much data for declared Content-Lengthが発生する

makoxti

総合スコア32

Pythonista

Pythonistaは、iOS上でPythonプログラミングができる開発アプリです。さらに、Pythonの関数・変数などを自動で補完する便利なコードエディタや、PythonスクリプトをiOS上で多様な形で機能させる各種機能も内包しています。

0グッド

0クリップ

投稿2022/03/20 08:07

編集2022/03/21 02:12

OS

  • M1Mac

言語

  • Python

Framework

  • FastAPI

git

事象

  • 削除用のAPIを叩くと、以下のエラーが発生する
    • h11._util.LocalProtocolError: Too much data for declared Content-Length

リクエストURL

curl -X 'DELETE'
'http://127.0.0.1:8081/blog/2'
-H 'accept: /'

ソースコード

  • 事象対象のメソッドはdeleteメソッドです。

python

1import re 2from fastapi import FastAPI, Depends, status, Response, HTTPException 3from sqlalchemy.orm import Session 4 5from .blog_code.schemas import Blog 6from .blog_code.models import Base 7from .blog_code import models 8from .blog_code.database import engine, SessionLocal 9 10# python3コマンド実行用 11# from blog_code.schemas import Blog 12# from blog_code.models import Base 13# from blog_code import models 14# from blog_code.database import engine, SessionLocal 15 16# テーブルを作成 17Base.metadata.create_all(bind=engine) 18 19app = FastAPI() 20 21 22def get_db(): 23 db = SessionLocal() 24 25 try: 26 yield db 27 finally: 28 db.close() 29 30 31@app.post("/blog", status_code=status.HTTP_201_CREATED) 32def create(blog: Blog, db: Session = Depends(get_db)): 33 new_blog = models.Blog(title=blog.title, body=blog.body) 34 db.add(new_blog) 35 db.commit() 36 db.refresh(new_blog) 37 return new_blog 38 39 40@app.get("/blog") 41def all_fetch(db: Session = Depends(get_db)): 42 blogs = db.query(models.Blog).all() 43 return blogs 44 45 46@app.get("/blog/{id}", status_code=status.HTTP_200_OK) 47def show(id: int, response: Response, db: Session = Depends(get_db)): 48 blog = db.query(models.Blog).filter(models.Blog.id == id).first() 49 if not blog: 50 raise HTTPException( 51 status_code=status.HTTP_404_NOT_FOUND, 52 detail=f"Blog with the id {id} is not available", 53 ) 54 return blog 55 56 57@app.delete("/blog/{id}", status_code=status.HTTP_204_NO_CONTENT) 58def delete(id: int, db: Session = Depends(get_db)): 59 blog = db.query(models.Blog).filter(models.Blog.id == id) 60 if not blog.first(): 61 raise HTTPException( 62 status_code=status.HTTP_404_NOT_FOUND, 63 detail=f"Blog with the id {id} is not available", 64 ) 65 blog.delete(synchronize_session=False) 66 db.commit() 67 68 return "Deletion completed" 69 70 71@app.put("/blog/{id}", status_code=status.HTTP_202_ACCEPTED) 72def update(id, request: Blog, db: Session = Depends(get_db)): 73 blog = db.query(models.Blog).filter(models.Blog.id == id) 74 if not blog.first(): 75 raise HTTPException( 76 status_code=status.HTTP_404_NOT_FOUND, 77 detail=f"Blog with the id {id} is not available", 78 ) 79 blog.update({"title": request.title, "body": request.body}) 80 db.commit() 81 82 return "Update completed" 83

エラーログ

ERROR: Exception in ASGI application Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi result = await app(self.scope, self.receive, self.send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__ return await self.app(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/applications.py", line 261, in __call__ await super().__call__(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__ await self.middleware_stack(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__ raise exc File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__ await self.app(scope, receive, _send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__ raise exc File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__ await self.app(scope, receive, sender) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__ raise e File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__ await self.app(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__ await route.handle(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle await self.app(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 64, in app await response(scope, receive, send) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/responses.py", line 156, in __call__ await send({"type": "http.response.body", "body": self.body}) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/exceptions.py", line 68, in sender await send(message) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 156, in _send await send(message) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 462, in send output = self.conn.send(event) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/h11/_connection.py", line 510, in send data_list = self.send_with_data_passthrough(event) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/h11/_connection.py", line 543, in send_with_data_passthrough writer(event, data_list.append) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/h11/_writers.py", line 65, in __call__ self.send_data(event.data, write) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/h11/_writers.py", line 91, in send_data raise LocalProtocolError("Too much data for declared Content-Length") h11._util.LocalProtocolError: Too much data for declared Content-Length

わかる方いらっしゃいましたらご教授お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Qiitaに移行しました。

投稿2022/03/20 17:12

makoxti

総合スコア32

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問